5

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!

1
  • 1
    $('.food').find('a').addClass('myClass') should be the fastest way in jquery. PS: edited, misread add a class to all the <a> elements Commented Sep 28, 2013 at 15:56

4 Answers 4

18

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()

Sign up to request clarification or add additional context in comments.

1 Comment

.find() is supposed to be a little bit faster, if it's what mean OP by 'quickest': jsperf.com/find-vs-descendant-selector/36
2

JQuery comes with addClass() and removeClass() to add or remove CSS class dynamically. For example,

$(‘.food′).addClass(‘ClassName’); – Add a “ClassName’ css class to elements that contain class of food

If you want to remove a class from your div, you can use the following:

$(‘.food′).removeClass(‘ClassName’); - Remove a “ClassName’ css class from elements that contain class of food

5 Comments

Is jQuery becoming synonym with Javascript..? I was talking to a dev I'm working with yesterday, I said that since we're merely using jQuery for DOM selection, we could remove it to remove a dependency, and he basically implied I was crazy for even proposing not using jQuery... (not criticizing your response, just a question)
@nbrogi you are right, OP tagged with jQuery so we all presume he accepts jQuery as a solution but a javascript one would be faster for sure
@nbrogi No, JQuery is not the synonym of Javascript. JavaScript is a language. jQuery is a library built with JavaScript. It includes many common web tasks to help developing websites. So, you can use javascript instead of JQuery, if you prefer. However, most of the time, JQuery is more convenient.
@A. Wolff Oh, yeah definitely, was just wondering in general, more that regarding the response itself.
@Holmes: I know, just speaking figuratively, that's why I wrote "is jQuery becoming", not "is it" ;-)
2

If you don't want to use jQuery:

var links = document.querySelectorAll('.food a');
[].forEach.call(links, function(item) {
  item.classList.add('myClass');
});

Comments

0

Add a class in specific div:

$("#divData").addClass("className");

Remove a class in specific div:

$( "#divData" ).removeClass( "className" );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.