Is it possible, after declaring two vars, to use them together to call a function in jQuery?
like:
var test1 = $('#mytest1');
var test2 = $('#mytest2');
test1,test1.fadeOut(100);
Yes. You can just include the selectors within quotes to the $ function:
$('#mytest1, #mytest2').fadeOut(2000);
#mytest1, #mytest2 {
width: 50px;
height: 50px;
margin: 5px;
background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="mytest1"></div>
<div id="mytest2"></div>
If you wish to use the existing variables you've already declared, you can use .add() as noted in this answer:
var test1 = $('#mytest1');
var test2 = $('#mytest2');
$(test1).add(test2).fadeOut(2000);
#mytest1, #mytest2 {
width: 50px;
height: 50px;
margin: 5px;
background-color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="mytest1"></div>
<div id="mytest2"></div>
If you don't already have two variables, just select both. Ref. http://learn.jquery.com/using-jquery-core/selecting-elements/#selecting-elements-with-a-comma-separated-list-of-selectors
$('#mytest1, #mytest2').fadeOut();
If you already have both in two separate variables, you can combine them. Ref. http://api.jquery.com/add/
test1.add(test2).fadeOut();
var1, var2.method(), it will sayExpected an assignment or function call and instead saw an expression