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.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With