Sush
Sush

Reputation: 25

Building HTML in javascript

I am trying to build HTML in my javascript file this way.I need to add one more classname to my class attribute. But this classname is a javascript variable. I am not sure how to add this variable so that javascript interpolates this.

  function build_html(sku)
  {
     //need to add sku as my classname here
     tmpString += "<a href=\"javascript: void(0)\" class=\"cart sku\">ADD TO CART</a>";
  }

TIA

Upvotes: 0

Views: 215

Answers (2)

Mithrandir
Mithrandir

Reputation: 25397

Just use the + operator to concatenate strings:

  function build_html(sku)
  {
     //need to add sku as my classname here
     tmpString += "<a href=\"javascript: void(0)\" class=\"cart " + sku + "\">ADD TO CART</a>";
  }

Upvotes: 1

Adil
Adil

Reputation: 148180

You can concatenate your html parts to add class name in your html,

Live Demo

tmpString = "";
classVar = "cart sku";
tmpString += "<a href=\"javascript: void(0)\" class=\""+classVar +"\">ADD TO CART</a>";

Upvotes: 1

Related Questions