I have DOM elements whose id may contain '/' in it. I want to use following formate, as I need all elements having the id for Ex:
jQuery('[id=activities/_terp_count]') //returns null
Note that ,
jQuery('#activities/_terp_count') //returns null
I have DOM elements whose id may contain '/' in it. I want to use following formate, as I need all elements having the id for Ex:
jQuery('[id=activities/_terp_count]') //returns null
Note that ,
jQuery('#activities/_terp_count') //returns null
The above solution works for me, but it's worth noting that there are advantages to the native method of getting an element by id: that is, document.getElementById('strin/g') will work here in cases where jQuery("strin/g") will fail (because jQuery uses a token parser that chokes on "/" without special treatment.escaping). To get the jQuery wrapped version of that element:
jQuery(document.getElementById('activities/_terp_count'));
In fact, if you can't always control exactly what's going into the actual id (say, if you're trying to search a page for an element with the id of something that was specified in the url #hash) then, a) be careful, because that can be dangerous and b) note that jQuery(document.getElementById(window.location.has.replace("#",''))); will work much more reliably than jQuery("#"+window.location.has.replace("#",'')); because it won't break as easily.
Example:
jQuery(document.getElementById("_=_")) //<--- null, or the actual element, if you have one which said crazy, non-compliant id
jQuery("#_=_") //<--Uncaught Error: Syntax error, unrecognized expression: #_=_
If you don't want your users to be able to break your code just by entering in a particular url parameter or hash, avoid that use case if you can, and if you can't, use the native ID element.
You can use the normal ID selector if you escape the /
sign - jQuery requires that for special characters (though, /
doesn't have a spacial meaning in jQuery or CSS, as far as I know)
$('#activities\\/_terp_count')
jQuery('[id=activities/_terp_count]')
should indeed find it (example - http://jsfiddle.net/jXsvA/), although it's going to be slower than a direct search by ID. It'll be faster if you can limit the selector some way, such as jQuery('div[id*=activities/_terp_count]')
if you know the tags are always <div>
's for example.