jQuery Selector is Referencing CSS Elements, Why?

Hi guys, I have recently had this question so thought I would share it with you!


“jQuery Selector is Referencing CSS Elements, Why?”


jQuery elements may include css naming schemes in IDs and they may be are interpreted as CSS notation
(particularly when using colons “:” and periods “.”). jQuery ID attributes may contain periods, colons, letters, numbers, hyphens, and underscores. The answer is to escape these characters by placing two backslashes in front of them.


For example:

// Does not work
// Works!
// Does not work
// Works!

Fix it with a function!

function jq(myid) { 
	return '#' + myid.replace(/(:|.)/g,'\$1');

$( jq('some.id') )