4

I'm trying to set the background colour of a span using jquery. I've used all functions in the title and all give me an error:

SCRIPT438: Object doesn't support property or method 'css'/'attr'/'addClass'

Any idea how I can solve this? All I want is to change the bg colour however I prefer if I can set the class.

code:

var liList = ul.find('li span');
        $.each(liList, function(index, value){
            if(value.innerText == currPage){
                value.css('background-color', '#D94A38');
            }
        });
Jonny
  • 2,787
  • 10
  • 40
  • 62
  • 1
    As Aleksandr said, you need to show your code. We can't see what is wrong until we have something to work with! – AlienHoboken Jan 14 '13 at 12:13
  • @jerome.s yes I'm using it in the same function which works perfectly – Jonny Jan 14 '13 at 12:13
  • 1
    Watch out for innerText usage on the DOM element as that may [not be supported on all browsers](http://stackoverflow.com/questions/1359469/innertext-works-in-ie-but-not-in-firefox). You can use $(value)[.text()](http://api.jquery.com/text/) as an alternative. – diffa Jan 14 '13 at 12:24

3 Answers3

7

The each() function gives you DOM object not jQuery object

Change to

$(value).css('background-color', '#D94A38');

Your code would be

var liList = ul.find('li span');
    $.each(liList, function(index, value){
        if(value.innerText == currPage){
            $(value).css('background-color', '#D94A38');
        }
});
Adil
  • 146,340
  • 25
  • 209
  • 204
  • since @James Hill was the first to answer I have to accept his answer. but thanks anyway :) – Jonny Jan 14 '13 at 12:19
6

value is not a jQuery object (it's a DOM object).

This:

value.css('background-color', '#D94A38');

Should be:

$(value).css('background-color', '#D94A38');

Check out the .each() docs for additional information.

James Hill
  • 60,353
  • 20
  • 145
  • 161
0

i think if you replace value by $(value) your problem will solved

happyZZR1400
  • 2,387
  • 3
  • 25
  • 43