Reputation: 171
How to check if the first 3 li elements contain the class yes? Then, execute a function. Vanilla Js only, no jquery.
<ul>
<li class="yes">blah blah</li>
<li class="yes">blah blah</li>
<li class="yes">blah blah</li>
<li class="no">blah blah</li>
<li class="no">blah blah</li>
<li class="yes">blah blah</li>
<li class="yes">blah blah</li>
</ul>
Example logic:
if (li[0] && li[1] && li[2] has class = yes) {
alert("You win!");
}
else{
alert("You lose!");
}
Upvotes: 1
Views: 974
Reputation: 4738
Add an id="ul"
to the UL element then run:
var el=document.getElementById('ul');
var li=el.getElementsByTagName('li');
if(
/ yes /.test(' '+li[0].className+' ')&&
/ yes /.test(' '+li[1].className+' ')&&
/ yes /.test(' '+li[2].className+' ')
){
alert('you win');
}else{
alert("You lose!");
}
Upvotes: 0
Reputation: 193261
In addition to several great javascript solutions I will propose two more CSS-oriented options.
1). nth-child. You can try this approach with :nth-child
to select first three children:
var li = document.querySelectorAll('ul li.yes:nth-child(-n+3)');
if (li.length === 3) {
// all three first elements has class "yes"
}
The idea is that li.yes
will select elements with class "yes", and :nth-child(-n+3)
makes sure that returned elements are the first three only. The result is a multiplication of those two conditions. Then we just need to check that returned result has length 3.
2). sibling selectors. Or one more a little crazy version, but even shorter using sibling selector +
:
if (document.querySelector('ul li.yes:first-child + .yes + .yes')) {
// all three first elements has class "yes"
}
The idea behind this approach:
li
has class "yes"Entire selector ul li.yes:first-child + .yes + .yes
then returns the third li
element, only if above three conditions are met.
Upvotes: 4
Reputation: 253307
One approach:
// storing an Array of the first three <li> elements returned by
// document.querySelectorAll():
var firstThree = [].slice.call(document.querySelectorAll('li'), 0, 3);
// logging whether or not every (using Array.prototype.every())
// Array element's classList contains the class of 'yes':
console.log(firstThree.every(function(el) {
return el.classList.contains('yes');
}));
var firstThree = [].slice.call(document.querySelectorAll('li'), 0, 3);
console.log(firstThree.every(function(el) {
return el.classList.contains('yes');
}));
<ul>
<li class="yes">blah blah</li>
<li class="yes">blah blah</li>
<li class="yes">blah blah</li>
<li class="no">blah blah</li>
<li class="no">blah blah</li>
<li class="yes">blah blah</li>
<li class="yes">blah blah</li>
</ul>
References:
Upvotes: 2
Reputation: 1
Shouldnt be too tough, try:
var answer = true
var lis = document.querySelectorAll("ul > li") // Gets all the <lis> in all the uls
var classes = lis.map(function (e) {return e.className} //list of classnames
for (e in classes.splice(0,3)) { // runs a function on each of the first 3 elements
answer = (e == "yes") && answer // if any of the elements isnt "yes", answer will become false
}
if (answer) {
alert("You win!!!!!!!")
} else {
alert("You lose")
}
Upvotes: 0