Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does querySelectorAll support the period(.) character in an id?

Does querySelectorAll support the period(.) character in an id?

I mean if I append an element like below:

var div = document.createElement('div');
div.id='my.divid';
document.body.appendChild(div);

and then I use querySelectorAll like below:

document.querySelectorAll('#my.divid');

then I get nothing!

So period is legal character for id, and querySelectorAll is the official method which Firefox provided; I can't believe the method doesn't support period(.) in id. Did I make some mistake?

like image 294
user2155362 Avatar asked Jul 10 '13 05:07

user2155362


People also ask

What does the querySelectorAll () method do?

The Document method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors.

Does querySelector work with ID?

They do so in different ways. With a querySelector statement, you can select an element based on a CSS selector. This means you can select elements by ID, class, or any other type of selector. Using the getElementById method, you can only select an element by its ID.

What is the difference between getElementsByClassName and querySelectorAll?

querySelectorAll successfully removes the classes from all the elements, but getElementsByClassName only removes the classes from about half the elements.

How does the querySelector () method differ from querySelectorAll ()?

Differences: As seen above, querySelector() methodcan only be used to access a single element while querySelectorAll() method can be used to access all elements which match with a specified CSS selector. To return all matches, querySelectorAll has to be used, while to return a single match, querySelector is used.


2 Answers

You need to remember that . represents a class selector, so the selector #my.divid represents an element with the ID my and a class divid, not an element with the ID my.divid. So, the following element would be matched by your selector:

var div = document.createElement('div');
div.id = 'my';
div.className = 'divid';
document.body.appendChild(div);

If you need to select an element with the ID my.divid as you have given above, you need to escape the period:

document.querySelectorAll('#my\\.divid');

Note that the double backslash is because it's a JS selector string; in a CSS rule you would use a single backslash: #my\.divid

like image 68
BoltClock Avatar answered Sep 20 '22 14:09

BoltClock


A period in querySelectorAll means you are specifying a css class. In your case querySelectorAll is trying to find out a DOM element with id "my" and having a css class "divid". How will querySelectorAll know that this time you want element by id and not by class? It is up to you to have proper id atrribute so as to no to confuse the method. Though a period is allowed, the best practise is to avoid it most of the time so that you do not confuse other libraries like jquery etc.

like image 30
mohkhan Avatar answered Sep 19 '22 14:09

mohkhan