1

I want to add content to an ui-grid. It is some json data and sometimes there is no data available. Because of this I need the if clause:

if (typeof results.xyz[0] !== "undefined") {
$("#testing").append('<div class="ui-block-a" id="springboard2"><div class="springboardIcon"><a onclick="changeActorInfo('+results.xyz[0].id+')" href="#"><img src="http://www..."><span id="springboardLabelActors">'+results.xyz[0].name+'</span></a></div></div>');
            } else {} 

2 Problems:

1) Every time I load the data the content is added. But the previously added content should disappear.

2) The else{} clause is currently empty. When there is no json data available no content should be added.

How do I manage this? Thank you in advance!!

4

1 に答える 1

0

The .append() method will add content to a container. If you want to replace existing content, you can use .html() instead.

There is no need for the else clause, your if condition alone should suffice. If you have no data, nothing will be inserted into the page.

Here is your revised code:

if (typeof results.xyz[0] !== "undefined") {
    $("#testing").html('<div class="ui-block-a" id="springboard2"><div class="springboardIcon"><a onclick="changeActorInfo('+results.xyz[0].id+')" href="#"><img src="http://www..."><span id="springboardLabelActors">'+results.xyz[0].name+'</span></a></div></div>');
}
于 2012-12-19T17:52:26.090 に答える