I have a line that appends a new div to an existing one. However I also want to add a class to the new div. The class's name is actually held in a var.
I am just unsure of the syntax of this - could anyone point me in the right direction at all?
var itemclass = "myclass";
$('#wrapper').append('<div class="itemclass"></div>');
So far I have tried:
$('#wrapper').append('<div class=" . itemclass . "></div>');
$('#wrapper').append('<div class=" & itemclass & "></div>');
To no effect... any help is appreciated!
I have a line that appends a new div to an existing one. However I also want to add a class to the new div. The class's name is actually held in a var.
I am just unsure of the syntax of this - could anyone point me in the right direction at all?
var itemclass = "myclass";
$('#wrapper').append('<div class="itemclass"></div>');
So far I have tried:
$('#wrapper').append('<div class=" . itemclass . "></div>');
$('#wrapper').append('<div class=" & itemclass & "></div>');
To no effect... any help is appreciated!
Share Improve this question asked Dec 29, 2012 at 4:29 MeltingDogMeltingDog 15.6k52 gold badges178 silver badges322 bronze badges 03 Answers
Reset to default 3You are concatenating a string, it is done like:
$('#wrapper').append('<div class="' + itemclass + '"></div>');
JS uses + to concatenate items, like this:
$('#wrapper').append('<div class="' + itemclass + '"></div>');
concatenation is the + sign in javascript, not a period like in PHP
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745289288a4620757.html
评论列表(0条)