0

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

3
  • 1
    This is done with very very basic JavaScript manipulation. If you don't know how to do that, you shouldn't be coding JavaScript just yet. Commented Dec 8, 2012 at 9:46
  • 1
    Use +. "Something "+ variable +" something" Commented Dec 8, 2012 at 9:47
  • As MadaraUchiha mentioned, it is one of the very basic tasks one should be able to perform. Maybe a little more practice in coding JavaScript is needed before you tackle some real project. Commented Dec 8, 2012 at 9:50

2 Answers 2

1

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>";
  }
Sign up to request clarification or add additional context in comments.

Comments

1

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>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.