0

I'm a bit new to JavaScript and I'm hitting a brick wall with this bit here.

I have a jsTree built with unique IDs based on filenames. For example,

<a id="node_:Folder4" href="#">Folder4</a>

Running a selector for

$("#node_:Folder4")

is returning [ ]. Here's a picture of the exact issue I'm seeing. Any thoughts?

https://i.stack.imgur.com/98cUQ.jpg

Cellivar
  • 568
  • 10
  • 27

2 Answers2

3

You need to escape the colon:

http://jsfiddle.net/MZA3b/1/

$('#node_\\:Folder4')

Not sure why, but I found this document here that says

ID and NAME tokens 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 (".").

http://www.w3.org/TR/REC-html40/types.html#type-name

I'm probably looking at deprecated document, but yes, try to avoid using special characters on attributes values

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • You are absolutely correct. HTML spec dictates that you can use almost any character in your ID, however in order to refer to it, "special" characters must be escaped so they are taken literally. This is also mentioned in the jQuery documentation in the selectors section. – Eli Sand Apr 03 '12 at 03:17
  • Ah excellent, this appears to be working properly. Thank you much! – Cellivar Apr 03 '12 at 03:18
2

You need to escape special characters - in this case, the colon.

$("#node_\\:Folder4")
mikevoermans
  • 3,967
  • 22
  • 27