Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript querySelectorAll data attribute value not equal to [duplicate]

My HTML tags have a [data-value] attribute which can be 0, 1, 2 and so on..

If I want to get the elements whose [data-value] is not equal to 0, how do I write my query selector?

I tried the following but they don't work:

element.querySelectorAll("not[data-value='0']");
element.querySelectorAll("[data-value!='0']");
like image 367
mark uy Avatar asked Oct 31 '18 03:10

mark uy


People also ask

How do I use querySelector with data attribute?

Use the querySelector method to get an element by data attribute, e.g. document. querySelector('[data-id="box1"]') . The querySelector method returns the first element that matches the provided selector or null if no element matches the selector in the document. Here is the HTML for the examples in this article.

Why querySelectorAll not working?

Originally Answered: Why is my querySelectorAll() method not working ? Make sure that the engine is executing the method call after the content, you are interested in, is loaded into the DOM. If you are constructing the query selector dynamically, then make sure it is correct during execution.

How do I get the value of a querySelectorAll?

To retrieve all copies of a specific element, you can simply pass the name of the element as its argument. You can also select elements by other attributes like target or value : // return all elements with target="_blank" document. querySelectorAll("[target=_blank]"); // return all elements with value="red" document.

What does the querySelectorAll () method do in JavaScript?

querySelectorAll() 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.


2 Answers

To avoid selecting other divs, use div[data-value]:not([data-value="0"]):

console.log(
    document.querySelectorAll('div[data-value]:not([data-value="0"])')
);
<div data-value="-1">0</div>
<div data-value="0">0</div>
<div data-value="1">1</div>
<div data-value="2">2</div>
<div>3</div>

This selects all divs with data-value and its value is NOT 0.

like image 64
Rafael Avatar answered Oct 03 '22 08:10

Rafael


Use the selector string 'div:not([data-value="0"])':

document.querySelectorAll('div:not([data-value="0"])')
  .forEach(div => console.log(div));
<div data-value="0">0</div>
<div data-value="1">1</div>
<div data-value="2">2</div>

You can't use [attrib!=value] in vanilla Javascript/CSS, unfortunately, you have to use :not instead. (though, you can use [attrib!=value] in jQuery)

To select <div>s which have additionally have a data-value attribute, but have one which is not 0, add [data-value] after the initial div in the selector string:

document.querySelectorAll('div[data-value]:not([data-value="0"])')
  .forEach(div => console.log(div));
<div data-value="0">0</div>
<div data-value="1">1</div>
<div data-value="2">2</div>
<div>3</div>
like image 38
CertainPerformance Avatar answered Oct 03 '22 06:10

CertainPerformance