Yasin Yörük
Yasin Yörük

Reputation: 1539

Odd class not working with loaded table by jquery

I'm trying to find the solution but still can't find and I have to ask this.odd or even class not working with loaded content by AJAX

HTML :

<table id="products"></table>

Jquery :

$(function () {$("#product-table tr:odd").addClass("odd") });

Everything's normal if I add rows into table manually.But when I load rows with jquery, odd class don't work.

Upvotes: 0

Views: 175

Answers (1)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382304

Your addClass function is called on the existing rows. This doesn't define a rule for new added rows.

If you can afford to limit the compatibility of this alternate row display, you should use CSS to do this because the CSS rules are executed each time the document is modified :

#product-table tr:nth-child(odd) {
    // what you would have put in the odd class
}

To be more compatible, you can also call your existing code from the callback you give to the ajax loading function :

$('someSelector').load(someUrl, function(){
   $("#product-table tr:odd").addClass("odd") 
});

Upvotes: 1

Related Questions