views:

368

answers:

8

In the DOM I have several Elements which have no ID, but instead an CSS class assigned. Like this:

<td class="foobar">...</td>

How could I access this element easily with jQuery?

+11  A: 

You'd do this:

$(".foobar")

The docs are here...

Nat Ryall
what does that dot mean?
HelloMoon
That means **class** , simply.
Moayad Mardini
It's CSS syntax for "class". Just like '#' is CSS syntax for "id".
Nat Ryall
+3  A: 

Use a dot. $(".foobar")

JacobM
+2  A: 
<div class="foo">bar</div>
<div class="foo">baz</div>

$('div.foo');
$('.foo');
// These are two of the 7 gajillion valid ways to access the .foo elements

Also, read the docs.

Reinis I.
+4  A: 
$('.foobar')

gets you an array of all elements with that class.

But seriously, RTFM. http://docs.jquery.com/Selectors/class#class

Matt Ball
+6  A: 

jQuery uses CSS Selectors.

So, your selector would be the same as how you access the item when writing CSS styles for it.

$('.foobar') //selects elements with class "foobar"
$('#foobar') //selects elements with the id "foobar"
$('div.foobar') //selects divs with the class "foobar"

etc.

More at: http://docs.jquery.com/Selectors

Chad
+2  A: 

You need a ID on your object, else all objects with "foobar" will affected. Try:

<td class="foobar" id="myFoobar">...</td>

then access with $("#myFoobar)

Cesar
+9  A: 

The first answer of everyone will be $(".foobar") or $("td.foobar") But there is a better way to select element/s especially when you are selecting them by there class.

By providing a context to the selector, you give it an element to start searching within so that it doesn't have to traverse the whole of the DOM.

in this case you write

$(".foobar", $("table"))

see an Extensive explanation on jQuery context

see also 25 excellent tips to improve your jquery (#9)

adardesign
+1 for the excellent resources. I'm learning from the 25 tips right now...
Steve Wortham
I know that `$('table')` maps directly to the DOM method `getElementsByTagName()`, but are more complex selectors like `$('table .foobar')` optimized similarly? I wouldn't be surprised if they did; regardless, unless your DOM is already so huge that it's slowing the browser significantly, you might as well just write `$('.foobar')` or `$('table .foobar')` in the interest of readability, maintainability, and least surprise. In almost every case, optimization should be a very low priority.
Matt Ball
In any case, searching for elements by classes has always been a little slow. True, the only time the user would ever notice is if it's a very repetitive task (inside mouseovers perhaps) and/or if the page is huge. But my opinion is that if you're searching by class names then you should specify the context parameter to speed it up. However, if you're searching by id (which is much much faster), then skip the context parameter.
Steve Wortham
Matt, I'm still not sure what the context parameter is doing behind the scenes. But from my own testing, writing a selector with a tag name or even an id in front of the class name has zero performance benefit. And in fact, including the tag name in the context parameter doesn't really help either. The thing that does help is specifying an id in the context parameter like I suggest in my answer.
Steve Wortham
+4  A: 

The simplest approach would be to do this:

$(".foobar")

However, it's not necessarily the fastest approach. This will return every element in the DOM with the class "foobar". But behind the scenes it's having to traverse through every single element in the DOM to get this list of elements.

Selecting elements with CSS classes through Javascript has always been an inherently slow operation (especially in large pages). Albeit, if you're only doing this once then we're probably talking about imperceivable milliseconds most of the time, but there is a faster way. As a rule of thumb, using an id in a CSS or jQuery selector is much faster than specifying a class. So whenever possible, use an id to narrow the number of elements the Javascript has to traverse. For example, if you had an id on the table like this:

<table id="mytable">
  <tr>
    <td class="foobar">
    </td>
  </tr>
</table>

Then you could do something more like this utilizing the optional context parameter:

$(".foobar", "#mytable")

This will search for all elements with a class of foobar inside mytable. And this should take considerably less time to do. There could easily be a 2X speed improvement or more depending on the relative size of the context compared to the rest of the page.

Resources:

Steve Wortham