4

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
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Shanta
  • 262
  • 1
  • 4
  • 15
  • 5
    ID attributes cannot contain forward slashes. It's not surprising jQuery has a hard time finding such elements. That said, there might still be a way to get the element with that ID. Also, "all elements with that ID" Note that you should only have one such element. – BoltClock Nov 22 '10 at 07:15
  • 3
    An `id` "must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").". The `/` is an invalid character. – kennytm Nov 22 '10 at 07:15
  • 1
    Please refer to: http://stackoverflow.com/questions/70579/what-is-a-valid-value-for-id-attributes-in-html – Kevin Nov 22 '10 at 07:16
  • but this FORMATE is the requirement of my project! If there is anyway. – Shanta Nov 22 '10 at 07:18
  • 1
    Your first code works fine on Firefox 4, Safari 5 and IE 8 http://jsbin.com/ewexu – kennytm Nov 22 '10 at 07:30
  • ohh. @kennyTM Thanks for the information. but I need compatibility in all possible working browsers – Shanta Nov 22 '10 at 07:35
  • @Shanta: I believe it works for other browsers too, it's just that I have only checked these 3. (Checking for the rest is your job :) ) – kennytm Nov 22 '10 at 07:36

3 Answers3

7

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.

Dtipson
  • 1,564
  • 16
  • 21
  • 1
    this is the simplest solution and the most sensible one. Why bothering [escaping](http://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/) (that is always prone to errors in unexpected situations), when you can leverage that resposability to native javascript methods. – thebugfinder Feb 11 '15 at 02:09
5

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')
Kobi
  • 135,331
  • 41
  • 252
  • 292
4

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.

Michael Low
  • 24,276
  • 16
  • 82
  • 119
  • Your solution was helpful for me, but with quotes inside (around `id`): `jQuery('[id="activities/_terp_count"]')`. Without it I've got a `Syntax error, unrecognized expression...`. – Boolean_Type Dec 20 '16 at 10:38