Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a div with Class using jquery

Tags:

jquery

I have a div with Class. For example

<div class="button check"></div>.The css is defined for both 'button','check'. I want to access the above div through jquery and write something in the div.I tried with

$('.button check').html("sample data");

I do not see anything being written when I run the page.

Please help me.

like image 774
user269527 Avatar asked Feb 09 '10 14:02

user269527


1 Answers

Just chain the CSS classes together, separated by periods:

$('.button.check').html("sample data");

You will also see better performance in some browsers, by specifying the tag name as well:

$('div.button.check').html("sample data");

Update: After reading Brian's answer, I re-read the original question I realized you might be under the impression you need to use both to reference the div. If you had:

<div class="button cancel">Cancel</div>
<div class="button check">Check</div>

Then chaining selectors (i.e. .button.cancel) would make sense. However, if it was the only div on the page or you wanted all the divs with button class, you don't need both classes:

<div class="button check">Check</div>

This would select it fine:

$('div.check').html("sample data");
like image 64
Doug Neiner Avatar answered Oct 03 '22 05:10

Doug Neiner