I am using a jQuery click function and would like to know if I can use the term 'this' to select the nav element and another selector at the same here's my code:
$('#nav').click(function() {
$(this, '#anotherSelector').hide();
});
This doesn't work. It selects the #anotherSelector
and not the #nav
element as well. What am I doing wrong?
Many thanks in advance.
You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.
Approach: Select the ID's of different element and then use each() method to apply the CSS property on all selected ID's element. Then use css() method to set the background color to pink to all selected elements. Display the text which indicates the multiple ID selectors.
To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.
Use .add()
to add another selector into your set of elements you want to deal with, like this:
$('#nav').click(function() {
$(this).add('#anotherSelector').hide();
});
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