Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select certain HTML-Element in div

I want to select certain HTML elements in a DIV. Something like this:

$("#foo table,tr,td,span,p" ).addClass( Myclass );

But it adds the class also outside of DIV "#foo". I.e. only the elements in div "#foo" are the class "Myclass" to add. Thanks.

like image 233
user3417601 Avatar asked Dec 06 '22 00:12

user3417601


1 Answers

Problem: Comma , separates autonomous selectors. You have to select #foo first, then select your inner elements table, tr, td, span, p.

You can use context attribute:

$("table, tr, td, span, p", "#foo").addClass("Myclass");

or you can chain your selectors:

$("#foo").find("table, tr, td, span, p").addClass("Myclass");
like image 171
Kate Miháliková Avatar answered Dec 26 '22 10:12

Kate Miháliková