Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  173] [ 5]  / answers: 1 / hits: 19298  / 6 Years ago, wed, october 31, 2018, 12:00:00

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']);

More From » html

 Answers
7

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.


[#53205] Sunday, October 28, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
maya

Total Points: 418
Total Questions: 116
Total Answers: 112

Location: Mauritania
Member since Sun, Oct 17, 2021
3 Years ago
;