1

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?

2
  • What do you mean by "the actual elements data"? The text within those elements? Are they inputs, and you want their values? Do they have .data() attributes that you want to use? Can you should us some of the elements, and the output you'd like to see from those elements? Commented Jun 4, 2015 at 17:46
  • I meant the elements as string, not elements data. Edited. Commented Jun 4, 2015 at 17:50

1 Answer 1

1

Use the outerHTML of the element being appended:

$.each($elements, function($i, $element){
    $newElements.push('<li class="'+ $classes[$i]+'">' + $element[0].outerHTML +'</li>');
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.