javascript - JQuery: append div with class that is a var - Stack Overflow

I have a line that appends a new div to an existing one. However I also want to add a class to the new

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 0
Add a ment  | 

3 Answers 3

Reset to default 3

You 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

相关推荐

  • javascript - JQuery: append div with class that is a var - Stack Overflow

    I have a line that appends a new div to an existing one. However I also want to add a class to the new

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信