1

In an ASP.NET web page, I have a table that shows results from a database query. In the same page, I have a form that adds a row to the database as well as displays the row at the end of table. I used jQuery ajax to insert the record to the database and if it's successful then I created a <tr> <td> ... </td> </tr> element and appended this element to the table using jQuery. This works fine. Now I want to make the table rows clickable. I tried it using $('#table-name tr').click(function () { alert ("hello"); });

Click function doesn't work for the rows that I added using the form. Is there any workaround? How can I make all rows clickable?

3 Answers 3

4

$('#table-name tr') returns only the <tr> elements that exist when you call that selector. If you add new rows dynamically, they won't be accounted for.

jQuery has special syntax to take care of dynamically generated elements:

$('#table-name').on('click', 'tr', function() {
    alert('Hello');
});
Sign up to request clarification or add additional context in comments.

Comments

1

Use jquery on instead of click. The problem is that click events are only attached when the page loads.

$('#table-name').on('click', 'tr', function() {alert('hello');});

Jquery on method waits for events to propagate up from child elements.

Comments

1

The reason that the click event does not work for those elements is that the event handler was issued only once, before the elements existed on the DOM.

In order to have the click event exist for elements which are added in the future as well, you should use on, this will allow the handler to be assigned to the dynamically added elements. Make sure to include the 'tr' which will create a delegate function to handle the event for future elements.

$('#table-name tr').on('click', 'tr', function () { alert ("hello"); });

Another option would be to assign the click event to the element before you dynamically add it to the page.

success: function(results){
 var newRow = document.createElement("tr");
 //manipulate newRow with results
 $(newRow).click(function(){ alert("hello") });
 //append newRow
}

6 Comments

.on('click') and .click() do the same thing.
@Blender - on will allow future elements to have the click event, whereas click will not. Note that your answer uses on as well.
Not really. .on() isn't a drop-in replacement for .live(). Take a look here: jsfiddle.net/MU4X4
Travis... There's a big difference on using .on and .on :D eg: $(element).on('click',function(){ = .bind() while $(parent).on('click','el',function(){ = deprecated .live() so, take a closer look at the DOCS, "nice info out there find you will". (Yoda)
@Blender - good point, you were right. On needs an extra argument in order to produce the event for future elements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.