1

I'm trying to grab an element from a page which has an id starting with an integer.

$('#3|assets_main|ast_module|start-iso-date')

I get the following error

Uncaught Error: Syntax error, unrecognized expression: |assets_main|ast_module|start-iso-date

I am using jQuery 1.7.1, I understand that the id in general is poorly named but is there any reason why jQuery will has issues with ids that start with integers?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
mk_89
  • 2,692
  • 7
  • 44
  • 62
  • IDs are supposed to start with a letter. What you have is basically invalid. The error is to be expected. – VLAZ Oct 14 '16 at 15:52
  • 1
    Also, yeah, I just realised - are the `|` also part of the ID? Because those would also be invalid. – VLAZ Oct 14 '16 at 15:53
  • @vlaz not true in HTML5. Ids can contain any character at any position except spaces. – Rory McCrossan Oct 14 '16 at 15:53
  • @vlaz yes I understand that it is poorly named, but I do not have the luxury of refactoring the code. – mk_89 Oct 14 '16 at 15:57
  • @vlaz HTML5 allows anything but spaces https://www.w3.org/TR/html5/dom.html#the-id-attribute and HTML4 only disallowed special characters not digits – Matthew M. Osborn Oct 14 '16 at 16:00
  • Possible duplicate of [jquery selector doesnt accept pipe character |?](http://stackoverflow.com/questions/12306252/jquery-selector-doesnt-accept-pipe-character) – Jordan Gray Oct 14 '16 at 16:09

1 Answers1

4

In HTML5 the id can start with a numerical value, so your id is valid.

The issue is the pipe (|) characters in the selector; you need to escape them using \\:

$('#3\\|assets_main\\|ast_module\\|start-iso-date')

Working example

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339