Reputation: 2074
I have multiple buttons in my HTML, and I only want to select those who hasn't and id attribute.
To select all buttons I use this script, which returns all buttons :
var buttonNodesList = document.querySelectorAll('button');
I googled about it, and I found that to get all buttons who has an id, I've to use a script like this :
var buttonNodesList = document.querySelectorAll('button[id]');
So isn't there any method to return thos buttons who has no id ?
Upvotes: 1
Views: 91
Reputation: 141829
In many modern browsers you can use :not:
var buttonNodesList = document.querySelectorAll('button:not([id])');
If you want wider browser support, you can use something like this:
var buttonNodesList = [].slice.call(document.getElementsByTagName('button'),0)
.filter( function(button) {
return button.getAttribute('id') === null;
});
Which leaves you with an array, rather than a NodeList and supports the most browsers only if you have a shim for filter.
Upvotes: 3