I append the following html:
<span sytle="color:#000000" />
The color value is stored in a variable:
var my_color = “#000000”;
var a = $(“#a”);
Below is what I have tried:
a.append('<span style="color: ' + my_color + '"</span>');
Is this correct?
You’re missing the closing bracket on the span element:
Because there’s no content within the span, are you sure that it’s failing? Appending a
spanto the#aelement wont change the color of the text within#a. An alternative might be to use:Also, just in case, make sure your script is executing after the
#aelement has been added to the DOM, otherwise your selection will not be selecting any elements, and appending will fail.You can simply wrap your code in a
document.readycallback with the following code: