Check for the existence of an element by the Value attribute?

1

I basically need to check if a given div exists inside a list, the structure looks like this:

<div class="content" id="divCheck">
   <div class="filho" value="1"></div>
   <div class="filho" value="2"></div>
   <div class="filho" value="3"></div>
</div>

<div class="content">
   <div class="filho" value="1"></div>
   <div class="filho" value="2"></div>
   <div class="filho" value="3"></div>
</div>

How can I check if there is an element with a value in the div with divCheck ? Is it correct to each in this div or to do this with a simple%     

asked by anonymous 04.01.2017 / 22:36

1 answer

3

Do not need a loop. You can use a selector to find if there is an element with an attribute and / or a value:

var existsValue1 = document.querySelector('#divCheck div[value="1"]');
console.log(!!existsValue1); //true

var existsValue8 = document.querySelector('#divCheck div[value="8"]');
console.log(!!existsValue8); //false
<div class="content" id="divCheck">
   <div class="filho" value="1"></div>
   <div class="filho" value="2"></div>
   <div class="filho" value="3"></div>
</div>

<div class="content">
   <div class="filho" value="1"></div>
   <div class="filho" value="2"></div>
   <div class="filho" value="3"></div>
</div>
    
04.01.2017 / 22:58