2

Can I add an IF statement to a XML query with jQuery?

My query is as follows.

$("#page_all").live('pagebeforecreate', function() {
    $.get('http://ontariosheep.org/mobile/data/data_all.php',function(data){
            $('.content').empty();
            $(data).find('market').each(function(){

        var $market = $(this);
        html += '<div class="ui-block-a">' + $market.find('lambs').attr('head') + '</div>';     

    }
}

I'd like to add an if statement on line 6. So that 'html+=' only happens if "$market.find('lambs').attr('head')" is set.

Is this possible, if so how?

EDIT:

All entries do have a head attribute BUT the ones without any head are blank like so head=""

2
  • Yes, you can add an "if" in there. You're just adding it to a function; the function is the anonymous function being passed as a parameter to the each() function. Commented Jun 18, 2012 at 18:38
  • What happened when you tried? Commented Jun 18, 2012 at 18:43

1 Answer 1

5

You don't need an if statement, you can filter it before the .each:

$(data).find('market').filter(':has(lambs[head])').each(...
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't work. I think because the entries without any head still say head="" with nothing inside. Can the filter be altered to account for that. Like filter, if lamb[head]!=""
Yes, [head!=""]. If that doesn't work, you'll have to rely on an if statement within the .each
@KevinB plz help me out in these question stackoverflow.com/questions/18591761/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.