How can I addClass or add css properties to multiple elements(id's) which has variables assigned to them. The code works if I use directly $('#cc, #dd').addClass('blue')
but doesn't work if it has variables assigned to id's like $(cc, dd).addClass('blue')
. Please check the below code, the commented line works but not if it has variables.
$(document).ready(function() {
var cc = $("#cc"),
dd = $("#dd");
$("button").click(function() {
// $("#cc, #dd").addClass("blue");
$(cc, dd).addClass("blue");
$(cc, dd).css({
"color": "#2196f3",
"font-size": "30px"
});
$("h1, h2, p").addClass("blue");
});
});
.blue {
color: #2196f3;
font-size: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<div id="cc">hello 1</div>
<div id="dd">hello 2</div>
<h1>Heading 1</h1>
<h2>Heading 2</h2>
<p>This is a paragraph.</p>
<button>Add classes to elements</button>