1
<ul>
    <li>
        <span class="name">Tortilla de blé</span>
    </li>
</ul>

This is the original source. I need to change the background Color of just the text in the tag. I have used css property background-color for it. But it is changing the color of the whole list item. i have to change the background color only by using javascript.

var ea = document.getElementsByClassName('name');
for(var i = 0; i < ea.length; i++)
    ea[i].style.backgroundColor = "yellow";

(Changed the Older Script as that was not correct, mistakenly written)

My Result:

Original Image

Expected Result: What could i do to just highlight the text in the tag and not the whole tag.

Expected Image

I have produced the expected result by editing the source code like below:

<ul>
    <li>
        <span class="name"><font style="bakground-color:yellow">Tortilla de blé</font></span>
    </li>
</ul>

By Embedding font tag to the text which is not possible with the javascript. I have done this with the help of Inspect Element feature of Google Chrome

1
  • A point of clarification: in the 'expected result' picture, is the JS being applied only to the first list element? Commented Oct 5, 2011 at 10:07

4 Answers 4

3

Your js example is not valid and should not do anything ...

You need to set the style on each span element;

var ea = document.getElementsByClassName('name');
for(var i = 0; i < ea.length; i++)
        ea[i].style.backgroundColor = "yellow";
Sign up to request clarification or add additional context in comments.

5 Comments

fine, but i need to highlight just the text not the whole tag ... is this possible?
It will; jsfiddle.net/pe4bn unless the span is inheriting a style that overrides its default behaviour, e.g. display:block
club-sandwich.net/recettes/tuna-wrap-256.php Check this Page. I have created my example form this page.
I am trying to highlight some text on a web page which is from a different domain.
Setting 'background: yellow;' on the <span class="name"> elements in the Chrome inspector works fine on your club-sandwich.net page - so it should be settable in JavaScript as well.
2

Tried with jQuery, http://jsfiddle.net/sameerast/gT9eh/

1 Comment

club-sandwich.net/recettes/tuna-wrap-256.php Check this Page. I have created my example form this page
1

As explained above - your JS is not valid. For starters, the function name is 'getElementsByClassName' rather than 'getElementByClaassName'.

You then need to have it loop through the elements and set their properties individually. JQuery will give you a nice shortcut as outlined by Sameera.

Comments

1

sample from Alex K. :

because there was an error I had to add 2 brackets :

var ea = document.getElementsByClassName('name');
for(var i = 0; i < ea.length; i++)
        {ea[i].style.backgroundColor = "yellow";}

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.