N
N
Nikita2015-10-12 11:16:56
JavaScript
Nikita, 2015-10-12 11:16:56

How to apply a method to two jQuery variables at once?

The question is, there are two variables

var $name = $('#usrname'),
    $email = $('#email');


Under certain conditions, I need to apply .hide() to them, and I don’t really like the option below:
$name.hide()
$email.hide()


You cannot combine these variables through .add(), or you can, but you need to put it in the third variable, which you don’t want either.
Is it possible to solve such a problem?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Azim Kurt, 2015-10-12
@FranklinDKitamory

Based on your condition, the problem is solved in this way:

[$name,$email].map(function(item) {
     item.hide();
})

S
Sergey Lysogor, 2015-10-12
@serhioli

In your case - best practice would be $('#usrname, #email').hide()
More info here: https://api.jquery.com/multiple-selector/
Was happy to help :)

S
Sergey Goryachev, 2015-10-12
@webirus

I'm not very special, but it won't work like that?

var $form = $('#usrname, #email')
$form.hide()

A
Alexey Tsarapkin, 2015-10-12
@Dreamka

And to register the same class on the elements and do so?
$(".classname").hide();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question