Reputation:
for example, i have four classes with id of "1" to "4". Then i want to add "hidden" to id of "2" and "3".
with this code:
this.block.addClass("hidden");
The "hidden" added to all four classes with id of "1" to "4". How can i use this code to add "hidden" only to id of "2" and "3"?
I should say sorry if my question is so simple, it is the first day that i am coding with javascript language. Thanks a lot
Upvotes: 2
Views: 41
Reputation: 88
You can filter elements like @CertainPerformance said with the following code:
this.block.filter('#2, #3').addClass("hidden");
However, since it's your first day using JS and jQuery you may not know that you can also refer to elements by their IDs:
$('#2').addClass("hidden");
You can also select by attributes. For example, if you're using data-id it would be:
$('[data-id="2"]').addClass("hidden");
Upvotes: 1
Reputation: 370759
You can filter elements in a jQuery object with the .filter method. Use commas to indicate "or" between selector strings:
this.block.filter('#2, #3').addClass("hidden");
But assuming your HTML is valid and doesn't contain duplicate id
s, you shouldn't need this.block
at all, just select #2
and #3
immediately:
$('#2, #3').addClass("hidden");
Upvotes: 4