Honestly, this query is not a good one. First off, all ids should be unique on the page, so "div#first" can just be #first. Next, the last part of the query with "ol#items > [name$='first']" reeks of bad semantics. The only element allowed directly under "ol" is "li". "li" can hold whatever, but "ol" should only have "li" direct children. Also name is not a "li" attribute, so that isn't semantic either. Of course, I know that sometimes we are not allowed to touch the HTML, so you may have to just deal with it.
In order to match <div id=”myUniqueID$0$”>, the query would be:
$("div#myUnique\\$0\\$")
Looks silly, but the two slashes are necessary. When your selector sees "div#myUnique$0$", it will toss it out, saying it is invalid. So if you were to escape it once, "div#myUnique\$0\$", JavaScript is going to jump in, because "\" is a escape character for JavaScript as well. So when Javascript sees that, it will replace it with "div#myUnique$0$", which we already know our selector with toss out. Therefore, we need to escape the escape character. This way it will turn into this:
Thanks so much Daniel J Dominguez ! Last question...do you know of a second option to rewrite var el = $( "div#myUniqueID$0$" ) ?
What do you mean second option? $("div#myUnique\\$0\\$") should suffice, but you could also drop the div, so that it will become $("#myUnique\\$0\\$"). Besides that, without knowing the structure of the HTML, I couldn't suggest anything else. If you know that "myUniqueID" is definitely going to be unique, then you can use $('[id^="myUniqueID"]'). "^=" says to match just the beginning of the string, so everything after myUniqueID will be ignored. Just know that it will also match "myUniqueID$1$" and "myUniqueIDs"
Daniel J Dominguez
It grabs elements that match the following criteria:
divs with an id of "first"
<div id="first">
divs with a class of "first"
<div class="first">
any element that has an name attribute whose value ends in first and is immediately the child of an "ol" that has an id of items
<ol id="items"> <li name="first"> <li name="testfirst">Honestly, this query is not a good one. First off, all ids should be unique on the page, so "div#first" can just be #first. Next, the last part of the query with "ol#items > [name$='first']" reeks of bad semantics. The only element allowed directly under "ol" is "li". "li" can hold whatever, but "ol" should only have "li" direct children. Also name is not a "li" attribute, so that isn't semantic either. Of course, I know that sometimes we are not allowed to touch the HTML, so you may have to just deal with it.
$("div#myUnique\\$0\\$")Looks silly, but the two slashes are necessary. When your selector sees "div#myUnique$0$", it will toss it out, saying it is invalid. So if you were to escape it once, "div#myUnique\$0\$", JavaScript is going to jump in, because "\" is a escape character for JavaScript as well. So when Javascript sees that, it will replace it with "div#myUnique$0$", which we already know our selector with toss out. Therefore, we need to escape the escape character. This way it will turn into this: