Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery .css/.attr/.addClass not working on span tag

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');
            }
        });
like image 706
Jonny Avatar asked Dec 12 '22 18:12

Jonny


1 Answers

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');
        }
});
like image 145
Adil Avatar answered Dec 30 '22 00:12

Adil