Is there any way to assign a class to multiple variables in JQuery ?
let x = $('#FirstId'),
y = $('#SecondId');
I want to Add a class to both variables in a single statement ,something like this :
(x,y).addClass('Something');
CodePudding user response:
You can create an array and loop through each item in that array
let x = $('#FirstId'), y = $('#SecondId');
// Here you create an array with both variables and loop through that variable
[x, y].forEach(item => item.addClass('blue'));
.blue {
color: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id="FirstId">First Item</p>
<p id="SecondId">First Item</p>
CodePudding user response:
You can add directly from the ids like:
$("#FirstId, #SecondId").addClass('Something');
You can read more in the link.