Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery - if .find returns nothing

Tags:

jquery

xml

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...

like image 416
nsilva Avatar asked Sep 21 '12 09:09

nsilva


People also ask

What does jQuery find return if not found?

Definition and Usage The find() method returns undefined if no elements are found. The find() method does not execute the function for empty elements.

Does find return jQuery object?

find() returns an object even when there's no matching child element in the DOM. Save this question.

How check selector is empty in jQuery?

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.

What does jQuery selector return?

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).


1 Answers

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

like image 114
Steve Greatrex Avatar answered Sep 22 '22 11:09

Steve Greatrex