0

How would I go about writing conditionals for the below ajax that says if one of these fields returns empty don't return part of the string? Any help would be MOST appreciated:

$.ajax({
    url: "http://www.lcbcchurch.com/mobileJSON/events",
    dataType: "json",
    success: function (data) {
        eventResults(data);
    }
});

function eventResults(data) {
    for (var i = 0; i < data.length; i++) {
        // this will log all of the images url
        console.log(data[i]["event-image"]);
        // just access the part you want by it's name.
        $("#event-images").append("<a href='#" + data[i]["url_title"] + "' id='event-button' data-role='button'><img class='pics' src='" + data[i]["event-image"] + "' width='" + 156 + "' height='" + 74 + "'></a>");
        $(".wrapper").prepend("<div id='" + data[i]["url_title"] + "' class='event-detail' data-role='page'><div class='back-header' data-role='header'><a href='events.html' data-icon='delete' iconpos='notext'>Cancel</a><h1></h1><a href='" + data[i]["event-registration"] + "' data-icon='arrow-r' data-iconpos='right' data-theme='b'>Register</a></div><img src='" + data[i]["event-image"] + "' /><strong>DATE: " + data[i]["event-date"] + "</strong><strong>LOCATION: " + data[i]["event-location"] + "</strong><strong>DEADLINE: " + data[i]["event-deadline"] + "</strong>" + data[i]["event-description"] + "</div>");
        $("#event-slider").append("<a href='" + data[i]["event-image"] + "'></a>");

    }
    $('.loading-wrapper').empty();
    // Call the pics ready function
    eventPics();
}
2
  • 1
    can you reframe your question properly, Which of the fields returns empty? The data returned as part of your ajax call? Commented Mar 15, 2012 at 18:42
  • event-description, event-deadline and event location could be empty. The rest always has data. I also need to return this as on big block, not separate block as in the example below. Commented Mar 15, 2012 at 18:48

1 Answer 1

1

Something like this I think. Don't have time to test.

if (data[i]["url_title"] != undefined) {
    $("#event-images").append("<a href='#"+data[i]["url_title"]+"' id='event-button' data-role='button'><img class='pics' src='"+data[i]["event-image"]+"' width='"+156+"' height='"+74+"'></a>");
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the help. I need to return this as one big block, not as separate blocks. In other words, some of the fields are empty and I need to not post if they are. But they need to be returned all at once so they are in the correct order.
I'm not sure what you mean by "one big block".
So instead of checking one conditional and then fetching the data, I need to fetch the data and the check some conditionals within the string. If they don't return true, I need to remove part of the string with those fields in it. Make sense?
So in other words, it's not all or nothing. Part of the string can be true while the other part is false.
Then seperate your code out into multiple appends. Put what is conditional inside the if statement and what isn't outside. Your entire div does not have to be inside one append.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.