Jquery.css / .attr / .addClass does not work on span tag

I am trying to set the background color of a range using jquery. I used all the functions in the header and they all give me an error:

SCRIPT438: object does not support property or method 'css' / 'attr' / 'addClass'

Any idea how I can solve this? All I want is to change the color of bg, but 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'); } }); 
+4
source share
3 answers

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

It:

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

Must be:

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

See .each() docs for more information.

+6
source

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

Change to

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

Your code will be

 var liList = ul.find('li span'); $.each(liList, function(index, value){ if(value.innerText == currPage){ $(value).css('background-color', '#D94A38'); } }); 
+7
source

I think if you replace the value with $ (value), your problem will be solved.

0
source

All Articles