Wasim Shaikh
Wasim Shaikh

Reputation: 7030

How to apply class to first button of the div?

I have this html and I want to apply class to first button of the DIV.

<div class="button-container">
 <button class="save" />
 <button class="reset" />
</div>

I want to add class to the first button.

I want to use jQuery for this solution.

Upvotes: 1

Views: 14283

Answers (4)

James Black
James Black

Reputation: 41858

You could look at this: http://docs.jquery.com/Selectors/firstChild

$('div button:first-child').addClass(...)

Upvotes: 5

SleighBoy
SleighBoy

Reputation: 501

div.button-container > button:first-child { font-weight: bold; }

Source: http://www.w3.org/TR/CSS2/selector.html#first-child

Upvotes: 5

rahul
rahul

Reputation: 187110

Use the :first-child selector

$("div.button-container button:first-child" ).addClass ( "yourclass" );

Upvotes: 7

Mitchel Sellers
Mitchel Sellers

Reputation: 63136

You can add a second class like this.

<div class="button-container">
 <button class="save SecondClass" />
 <button class="reset" />
</div>

Upvotes: 4

Related Questions