0

I have got the following jquery code which appends some html elements to the <a> tag found inside a <li> tag.

Currently, i am hardcoding the class of the <ins> tag, but actually i need to pass a javascript variable (selected_class) as the the class name.

 if (colour[1] == "blue"){
  selected_class = "colour-icon1";
  }
  else if (colour[1] == "yellow")  {
   selected_class = "colour-icon2";
  }
  else if (colour[1] == "green")  {
   selected_class = "colour-icon3";
  }


      $j("li[name='"+node_name+"'] > a").append('<a><ins class="colour-icon1">&nbsp;</ins></a>');

My question is how do i pass a javascript variable (selected_class) as the the class name inside the <ins> tag?

3 Answers 3

4
$j("li[name='"+node_name+"'] > a").append('<a><ins class="' + selected_class + '">&nbsp;</ins></a>');

Should do the trick. You're already doing string concatenation in your selector:

li[name='"+node_name+"']
Sign up to request clarification or add additional context in comments.

Comments

3
var selected_class = ...;
var ins = $j("<ins>").addClass(selected_class);
var newLink = $j("<a>").append(ins).append("&nbsp;");
$j("li[name='" + node_name + "'] > a").append(newLink);

Comments

-1

Have a look at jQuery.addClass(). I believe it's what you're looking for.

I'm not adept at creating selectors yet so I can't help there. Though it seems Phil Klein has a solution for you.

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.