dnatoli
dnatoli

Reputation: 7012

Access an element by class using jQuery

I have a table with an ID of InstrumentListGrid. When a row is selected, it sets the class to ui-iggrid-activerow. I want to add a jQuery event on that row for when someone clicks it.

So far I have

$("#InstrumentListGrid tr.ui-iggrid-activerow").click(function (event) {
    alert("YAY!");
});

but that isn't firing. How do I bind to an element by class?

Upvotes: 3

Views: 178

Answers (2)

Peter Olson
Peter Olson

Reputation: 143017

It appears that ui-iggrid-activerow is dynamically added. Use the live() function:

$('#InstrumentListGrid tr.ui-iggrid-activerow').live('click', function() {
    alert('YAY!');
});

Upvotes: 0

David Wick
David Wick

Reputation: 7105

since the class is presumably added dynamically you should use .delegate()

$('#InstrumentListGrid').delegate('.ui-iggrid-activerow', 'click', function (e) {
    // do stuff.
});

Upvotes: 2

Related Questions