JQuery - if .find returns nothing

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 the .find function, which finds every additional one and displays the result on a web page.

 $(xmlExtras).find('extra').each(function(){ }); 

How would I start writing a function, so that if all the "extras" = enabled = "0" do something ...

+4
source share
4 answers

Refresh after reading the question correctly

You can check the length of the find result with an optional selector:

 var $enabledExtras = $(xmlExtras).find('extra[enabled="1"]'); if ($enabledExtras.length == 0) { //do something } 

JsFiddle works

+10
source

You must try.

 $(xmlExtras).find('extra').each(function(i){ if($(this).find('enabled) == 0) { do something... } else { } }); 

Additional information: in such a situation, you should try JSON.

0
source

Hi, I was pulled for a phone call, but here I gave this example: http://jsfiddle.net/H3eFM/1/

0
source

I hope the code below helps you

 var xmlText='<?xml version="1.0"?><extraslist><extra id="0" enabled="1" quantityavailable="2" displayindex="1"><extraname>Example 1</extraname></extra></extraslist>'; xmlDoc = $.parseXML(xmlText); $(xmlDoc).find('extra').each(function(){ //All your code goes here }); 
0
source

Source: https://habr.com/ru/post/1435471/


All Articles