So I have this arrangement in my page:
<div class="food">
<div>
<a href="#" class></a>
<a href="#" class></a>
</div>
<div>
<a href="#" class></a>
<a href="#" class></a>
</div>
</div>
How do I add a class to all the a elements inside my div.food? What is the shortest and quickest way to implement this?
Thanks!
Step 1) Add HTML:Add a class name to the div element with id="myDIV" (in this example we use a button to add the class).
To do that, first we create a class and assign it to HTML elements on which we want to apply CSS property. We can use className and classList property in JavaScript. Approach: The className property used to add a class in JavaScript.
Using . add() method: This method is used to add a class name to the selected element. Syntax: element.
To add class to all a
tag in div with class food
$('div.food a').addClass('className');
or
As commented by A. Wolff .find()
is faster
$('div.food').find('a').addClass('className');
or
To add class to all elements in div with class food
$('div.food *').addClass('className');
or
$('div.food').find('*').addClass('className');
.addClass()
.find()
also read .removeClass()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With