Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery select 'this' and another selector at same time

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.

like image 892
mtwallet Avatar asked Nov 26 '10 14:11

mtwallet


People also ask

Can we use multiple selectors in jQuery?

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.

How can use multiple ID selectors in jQuery?

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.

How do you select an element with a particular class selected?

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.


1 Answers

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();
});
like image 59
Nick Craver Avatar answered Oct 08 '22 16:10

Nick Craver