Reputation: 770
Got a problem. In the following fiddle I have tried to exemplify how I wish to check for if the variable "htmlstring" has already been appended, and if so, then not to append another. On the other hand if it haven't been appended, ofcourse append it.
http://jsfiddle.net/Lc5gdvge/6/
How do I make this work? All in all, what I need is a if-else code, that will check for previous appended variable. If none has been appended it will append, if the container already contains the appended variable "htmlstring" then it ofcourse shouldn't append a new one.
$(document).ready(function () {
$('.outerdiv').click(function () {
var htmlstring = $(this).contents().filter(function(){return this.nodeType == 3;}).text()
if ($('.innerdiv').innerhtml( jQuery.inArray(htmlstring, arr))){ /*this line is the problem*/
}
else {
$(this).find('div.innerdiv').append(htmlstring);}
})
});
Upvotes: 1
Views: 338
Reputation: 8168
You can use jQuery's length
to find if any content is present like
var checkContent = $(this).find('div.innerdiv').html().length;
if(checkContent> 0){
//contents are present
}
NOTE: You have a semi-colon missing in your
$('.outerdiv').click(function () {
});// missing semi-colon. Its good to use one
Upvotes: 1