Reputation: 6000
I have a div like this:
<div class='myclass'></div>
Sometimes i need to make these div dynamically and add another class to it. I do it like this
if(condition1)
appendClass = 'xyz'
if(condition2)
appendClass = 'abc'
if(condition3)
appendClass = 'usb'
$('main').append("<div class = myclass "+ appendClass +"/>")
but when i check this div with firebug i get this
<div class="myclass" xyz=""/>
which should have been
<div class="myclass xyz"/>
I tries using append.replace(/"/g, '')
but same result.
Please help!!
Upvotes: 1
Views: 103
Reputation: 1164
You can also do something like this
$("<div>").addClass("myclass " + appendClass).appendTo($(".main"))
Upvotes: 2
Reputation: 26163
You can mix single and double quotes to quote things in strings, like this...
$('.main').append("<div class='myclass " + appendClass + "' />")
Upvotes: 1
Reputation: 2456
$('main').append("<div class =' myclass "+ appendClass +"' />")
but my personal preference is:
$('main').append('<div class="myclass ' + appendClass + '"/>');
Upvotes: 7