Christian
Christian

Reputation: 9

How can I design text with "span" using .insertAdjacentHTML in Javascript?

This is what I think is has to look like:

    toggleText.insertAdjacentHTML('beforeBegin', '<span style="color:white">');
    toggleText.insertAdjacentHTML('beforeBegin', partvalue[partvalue.length-1]);
    toggleText.insertAdjacentHTML('beforeBegin', '</span>');

But when I execute the function, I get this in HTML:

<span style="color:white"></span>
"Text from partvalue.length-1"

This is what I would like to use, but I know it is not working like this. What can I do?:

    toggleText.insertAdjacentHTML('beforeBegin', '<span style="color:white">'partvalue[partvalue.length-1]'</span>');

Upvotes: 0

Views: 114

Answers (1)

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67187

You have to concatenate it properly in order to make it working,

 toggleText.insertAdjacentHTML('beforeBegin', 
       '<span style="color:white">' + partvalue[partvalue.length-1] + '</span>');

In javascript, for string concatenation + have to be used.

Upvotes: 2

Related Questions