How do you set a variable to be an empty, but workable Jquery object?

Posted by KallDrexx on Stack Overflow See other posts from Stack Overflow or by KallDrexx
Published on 2010-05-26T11:43:55Z Indexed on 2010/05/26 11:51 UTC
Read the original article Hit count: 207

Filed under:
|
|

Outside of a for I declare a variable using var list;. I use this variable inside of my for loop like so:

// add the html to the list
if (list == undefined)
    list = item;
else
    list.append(item.contents());

item is a cloned jquery object build from a $('list_template').clone(); call (list_template is a div with <li> elements inside). What I am doing is creating a list, which I will then appendTo() where I need it.

Right now this code works fine, but it doesn't seem right to me. Unfortunatly, I cannot seem to figure out how to correctly declare the list variable to be an empty Jquery object. I have tried both:

var list = $([]);
var list = $('');

Both of those cause the append to not work correctly (or as expected) with list.html() being null. Is there a way to initialize my variable to an empty jquery object, so all I have to do is list.append(item.contents()); without the if/else statements?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery