ameraz
ameraz

Reputation: 53

jQuery Print elements pushed into array

I have the following code:

var $element1 = $('#selector'),
    $element2...;

var $elements = [ $element1, $element2, $element3 ],
    $classes = ['class1','class2','class3'],
    $newElements = [];

$.each($elements, function($i, $element){
    $newElements.push('<li class="'+ $classes[$i]+'">' + $element +'</li>');
});

$(body).append( $newElements );

The output being:

<li class="class1">[object Object]</li>
<li class="class2">[object Object]</li>
<li class="class3">[object Object]</li>

How can I print the actual elements?

Upvotes: 1

Views: 49

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Use the outerHTML of the element being appended:

$.each($elements, function($i, $element){
    $newElements.push('<li class="'+ $classes[$i]+'">' + $element[0].outerHTML +'</li>');
});

Upvotes: 1

Related Questions