I have the following XML for example:
<?xml version="1.0"?>
<extraslist>
<extra
id="0"
enabled="1"
quantityavailable="2"
displayindex="1">
<extraname>Example 1</extraname>
<extradesc>Example 1 Description</extradesc>
</extra>
<extra
id="1"
enabled="1"
displayindex="2">
<extraname>Example 2</extraname>
<extradesc>Description 2</extradesc>
</extra>
</extraslist>
And a .find function that finds each extra and displays the result on a web page.
$(xmlExtras).find('extra').each(function(){
});
How would I go about writing a function so that if all 'extras' = enabled="0" to do something...
Definition and Usage The find() method returns undefined if no elements are found. The find() method does not execute the function for empty elements.
find() returns an object even when there's no matching child element in the DOM. Save this question.
This method can be used on this element to test if it is empty by using “:empty” selector. The “:empty” selector is used to select all the elements that have no children. It will return true if the element is empty, otherwise, return false.
The jQuery Object: The Wrapped Set: Selectors return a jQuery object known as the "wrapped set," which is an array-like structure that contains all the selected DOM elements. You can iterate over the wrapped set like an array or access individual elements via the indexer ($(sel)[0] for example).
Update after reading question properly
You can check the length of the find
result using an extra selector:
var $enabledExtras = $(xmlExtras).find('extra[enabled="1"]');
if ($enabledExtras.length == 0) {
//do something
}
Working jsFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With