2

Total noob here, trying to conditionally append some values to an html object. This is built from sample code I found so be kind...

$.getJSON('url to facebook json feed', function(fbresults){
    $.each(fbresults.data, function(){
        $('<div></div>')
        .append('<h1>' + this.from.name + '</h1>')
        .append('<p>' + this.story + '</p>')
        $if (typeof this.picture !== "undefined") {
            .append('<img src="' + this.picture + '">')};
        .appendTo('#facebook')
          });
     });
1
  • 2
    Come on, there is nothing like $if... Try to separate each append command and then add conditions to some of them. Commented Mar 3, 2013 at 22:58

4 Answers 4

3

jQuery's append method accepts multiple elements, including null objects like in:

$('<div>').append(
    $('<p>').text('First paragraph'),
    $('<p>').text('Second paragraph'),
    null,
    $('<p>').text('Third paragraph')
);

which is equivalent to

$('<div>').append(
    $('<p>').text('First paragraph')
).append(
    $('<p>').text('Second paragraph')
).append(
    null
).append(
    $('<p>').text('Third paragraph')
);

Please note that a null object would simply be ignored in the final DOM element.

For this reason you can adjust your code as follows:

$.getJSON('url to facebook json feed', function(fbresults){
    $.each(fbresults.data, function(){
        $('<div></div>').append(
            $('<h1>').html(this.from.name),
            $('<p>').html(this.story),
            (this.picture) ?
                $('<img>').attr('src', this.picture) :
                null
        ).appendTo('#facebook')
    });
});

The third element appended to the div is either the img element or null depending on this.picture being defined or not.

Sign up to request clarification or add additional context in comments.

Comments

2

You don't have to chain it all together:

$.getJSON('url to facebook json feed', function(fbresults){
    $.each(fbresults.data, function(){
        var element = $('<div></div>');
        element.append('<h1>' + this.from.name + '</h1>');
        element.append('<p>' + this.story + '</p>');

        if (typeof this.picture !== "undefined") {
            element.append('<img src="' + this.picture + '">')
        };
        element.appendTo('#facebook');
    });
});

It also is considered good practice to build what you want to append in a string and append it AFTER the $.each

2 Comments

Thank you very much, this worked perfectly! To tell the truth I'm not a programmer, just a designer. Thus the bad code. Time to take a class on javascript I think!
In this case I would totally recommend codecademy.com - I know several people who learned to code with this page. They provide courses for JavaScript as well as jQuery. Please feel free to accept the answer if it helped solving your problem.
1

jQuery is just a JavaScript library. You're still working with JavaScript:

$.getJSON('url to facebook json feed', function(fbresults){
    $.each(fbresults.data, function(){
        var $div = $('<div>');

        $('<h1>', {text: this.from.name}).appendTo($div);
        $('<p>', {text: this.story}).appendTo($div);

        if (this.picture) {
            $('<img>', {src: this.picture}).appendTo($div);
        }

        $div.appendTo('#facebook');
     });
});

Comments

0

Try something like this:

$.getJSON('url to facebook json feed', function(fbresults){
    $.each(fbresults.data, function(){
        var $wrapper = $('<div></div>')
            .append('<h1>' + this.from.name + '</h1>')
            .append('<p>' + this.story + '</p>');

        if (this.picture) {
            $wrapper.append('<img src="' + this.picture + '">');
        } else {
            $wrapper.appendTo('#facebook');
        }
    });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.