0

I am using jQuery and have element in DOM with id="22/1". When I try to create object by id in jQuery using $('#22/1') I am getting below error.

jquery.min.js:2 Uncaught Error: Syntax error, unrecognized expression: #22/1 at Function.ga.error (jquery.min.js:2)
at ga.tokenize (jquery.min.js:2)
at ga.select (jquery.min.js:2)
at Function.ga [as find] (jquery.min.js:2)
at r.fn.init.find (jquery.min.js:2)
at new r.fn.init (jquery.min.js:2)
at r (jquery.min.js:2)
at :1:1

So how do I can get element using jQuery? is this a but in jQuery? Please dont suggest to use $(document.getElementById('22/1')).

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 2
    You need to escape the `/`: `$('#22\\/1')` – Rory McCrossan Jun 22 '18 at 09:23
  • So how are you reading them? Assuming you're building a string selector, use `replace()`: `selectorreplace(/\//g, '\\\\/');` http://jsfiddle.net/be1n2fck/ – Rory McCrossan Jun 22 '18 at 09:44
  • yes this can work. and I'll have to modify lots of code for this. BTW I am not reading element by hard coding in jQuery. here is how I am getting id var val = $('#myDropDown').dropdown('get value'); var dataList = $('#some_prefix_id_'+val).data('some_data'); I belive the # symbole is to refere the elements id (http://api.jquery.com/id-selector/) Its also weird that if Html and most basic JS supports e.g. getElementById('22/1'). why escape char is required in jQuery while # is prefixed. – Pankaj Patel Jun 22 '18 at 10:03
  • jQuery also not supporting $('#this.this') !! – Pankaj Patel Jun 27 '18 at 07:43
  • Yes it does. If it doesnt work for you then youve done something wrong. Check the console for errors – Rory McCrossan Jun 27 '18 at 07:54

1 Answers1

5

Your ID is somewhat pretty weird.

To access your object, use jQuery attribute selector [...] :

let object = $('[id="22/1"]')
console.log(object[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="22/1"></div>
Zenoo
  • 12,670
  • 4
  • 45
  • 69