While going through official doc ,I am unable to understand that for matching the first div ,how they have written four backslash(\) ,In my opinion there should be two backslash.I am unable to find the valid logic for this reason
<div id="foo\bar"></div>
<div id="foo:bar"></div>
document.querySelector('#foo\\\\bar'); // It matches the first div
Yes, you can use it, and you can have href="#/name" . No escaping needed. In CSS, it needs to be escaped, e.g. using a selector like #\/name .
Use the document. querySelectorAll() method to get all elements whose id starts with a specific string, e.g. document. querySelectorAll('[id^="box"]') . The method returns a NodeList containing all the elements that match the provided selector.
You can use attr('id') in jQuery or the id property (or getAttribute('id') ) on the native DOM element. Show activity on this post. Show activity on this post. You can also use conventional .
querySelector() The Document method querySelector() returns the first Element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.
This is due to the browser escapes the backslash in the id attribute. So it will become
<div id="foo\\bar"></div>
So inorder to select the element we need to provide \\\\
to select the element.
var elements = document.querySelector('#foo\\\\bar');
console.log(elements);
var elements = document.querySelector('#foo\\bar');
console.log(elements);
<div id="foo\bar"></div>
<div id="foo:bar"></div>
Update based on comment You can read more information regarding this in following links:-
https://mathiasbynens.be/notes/css-escapes
https://www.w3.org/TR/CSS21/syndata.html#characters
You have two double backslashes. As you know, the backslashe is a "escape" character.
So your selector string become:
#foo\\bar <--- to have this string in a variable, you need to use '#foo\\\\bar'
Now, the double backslash that left (in the string) will be used (again as a escape character) by the querySelector.
You can checkout more details about how selectors can be written here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With