Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery addClass not working

I'm trying to use addClass to give me zebra-striped tables on my Joomla template. Im using the following code:

 <script>
  jQuery(function($) {
    $("tr:odd").addClass("odd");
  });
</script>

I've been able to use the tr:odd selector to add css to table rows dynamically, but when i use the addClass function it just doesnt (I checked the source code produced and none of the table rows have the class "odd").

Havn't a clue what I could be doing wrong, would appreciate any help.

like image 726
Chris Armstrong Avatar asked Jul 27 '09 13:07

Chris Armstrong


People also ask

What does addClass do in jQuery?

The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.

How do you check class is exists in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".


1 Answers

So you know, changes to the DOM with Javascript are not reflected when you view the source.

That code should work if your CSS looks like this...

tr.odd td
{
    background:#070;
}
like image 131
Josh Stodola Avatar answered Nov 02 '22 06:11

Josh Stodola