William Buttlicker
William Buttlicker

Reputation: 6000

Escaping single quote

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

Answers (4)

Brian
Brian

Reputation: 1164

You can also do something like this

$("<div>").addClass("myclass " + appendClass).appendTo($(".main"))

Upvotes: 2

Shinov T
Shinov T

Reputation: 862

try This

$( ".myclass").addClass(appendClass);

Upvotes: -1

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26163

You can mix single and double quotes to quote things in strings, like this...

$('.main').append("<div class='myclass " + appendClass + "' />")

Upvotes: 1

aletzo
aletzo

Reputation: 2456

$('main').append("<div class =' myclass "+ appendClass +"' />")

but my personal preference is:

$('main').append('<div class="myclass ' + appendClass + '"/>');

Upvotes: 7

Related Questions