1

I have some problem with append array to a html: I create an array with html, first element is "li", than "img" and last element is "/li"

it returns

<li></li>
<img src="...">

but i need:

<li>
  <img src="...">
</li>

here is jsfeedle

here js:

var items = [];
items.push( "<li class='cd-item'>" );
items.push( "bar" );
items.push("</li>");
var itm = items.join(''); 
$(".cd-items").append(itm);

what do i wrong?

Thanks!

Sorry for my English.

1
  • Please include your jQuery in your question; the demo is a nice bonus, but without the relevant, problematic, code in the question your question is incomplete. Commented Dec 13, 2014 at 16:40

2 Answers 2

2

Can use join()

var items = [];
items.push( "<li class='cd-item'>" );
items.push( "<img src='images/source.jpg'>" );
items.push("</li>");
 var html = items.join('');
$(".cd-items").append(html);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<ul class="cd-items">
    foo
</ul>

Sign up to request clarification or add additional context in comments.

Comments

1

you can change this line:

$(".cd-items").append(items);

to

$(".cd-items").html(items);

see append reference for more details.

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.