Stone Stone - 2 months ago 9
Javascript Question

add white space when create a div when prepend

I use this

$("#containerSeparateChart").prepend("<div class="+ key +"></div>")


to create dynamic
div
when I click a button depend on the key I create the name of the class, but now I want to add one more class for example
col-xs-6


I try this

$("#containerSeparateChart").prepend("<div class="+ key +"col-xs-6"+"></div>")


this add the class but, together like this
class="keynamecol-xs-6"
and I want a space between the name like this
class="keyname col-xs-6"


I try to use all this option


  • $("#containerSeparateChart").prepend("<div class="+ key +" col-xs-6"+"></div>")

  • $("#containerSeparateChart").prepend("<div class="+ key +"&nbsp;col-xs-6"+"></div>")

  • $("#containerSeparateChart").prepend("<div class="+ key + " "+"col-xs-6"+"></div>")



But all this case create something like this

class="keyname" "col-xs-6"


Any idea what I doing wrong!

thanks in advance!

Answer

Just a small issue with your quotes:

$("#containerSeparateChart").prepend('<div class="' + key + ' col-xs-6"></div>');

Example: https://jsfiddle.net/qv49p1az/1/


Side note: If you are not already, I would recommend using a code editor (IDE) such as Atom (http://atom.io). Most modern editors would typically pick up on syntax errors like this.