The main idea is: I have a list of beverage
in a div
and I'd like to check
if the beverages are out of stock or not
, and click
the first drink available
in stock. It needs to be in JavaScript (without JQuery)
The elements are in this hierarchy
:
Main div
containing all the drinks: class="ob-menu-items__items"
Each drink
will be in a H4
class="ob-menu-item__title"
If the product is out of stock
there will be a span
class="ob-menu-item__out-of-stock"
with the text " - Out of Stock"
So far I try this (and got stuck):
for (var i = 0; i < 7; i++) {
// iterating over each drink.
var drink = document.getElementsByClassName("ob-menu-item__title")[i];
if (document.getElementsByClassName(".ob-menu-item__out-of-stock").parents(drink).length == 1) {
// There's out of stock text
// Do nothing and go to the next drink
} else {
//The product is available. Clik the drink and exit the loop
document.getElementsByClassName("ob-menu-item__title")[i].click();
break;
}
}
Thanks!