FrancescoN FrancescoN - 7 months ago 29
Javascript Question

jquery append strange behavior

I have to build html elements to make my webpage.

var container_item_data = $('<div>').attr('class', 'item_data');
var item_name = $('h4').append('weeee');

container_item_data.append(item_name, 'doooo');
$('#products').append(container_item_data);


But I only get 'doooo' string inside the container_item_data.
How can I make it work?

Answer

I think you forgot the arrow brackets around the h4 tag. Change your var item_name line to this:

var item_name = $('<h4>').append('weeee');

Here's a fiddle I made to help explain:

https://jsfiddle.net/qe1862ju/