I want create a new <li> component, then add href, class, etc.. and finally place it in another element.
I thought this would work, but it doesn’t:
var points = [1, 2, 3, 4];
$.each(points , function(){
var $elm = $('<a>'+'Point:'+$(this)+'</a>');
$elm.css('point');
$elm.attr('href','http://somelink');
$('#points_list').append('<li>'+$elm+'</li>');
});
The html code is:
<ul id="points_list"/>
The output is:
That JavaScript is invalid (the first line is a syntax error).I see you’ve corrected it.I think this may be what you mean (I’ve had to guess at the
cssbit, I assume you meant to add that as a class) (live copy):But it can be simpler:
I’d also recommend looking up the
points_listonce and then reusing the reference:…since even looking things up by
idis not free.