temp-learn
temp-learn

Reputation: 537

jquery on Click event not firing

This is my script by which i am firing event

   $("#drg_table").closest('tr td').click(function(){     
                    alert('i am here');
                });

and this is my table

       <table name="drg_table">
<tbody>
<tr id="hospital_name" name="hospital_name">
<td>hospital_name</td>
<td>Average Covered Charges</td>
<td>Average Total Payments</td>
<td>Total Discharges</td>
</tr>
<tr id="GREEN CLINIC SURGICAL HOSPITAL" name="GREEN CLINIC SURGICAL HOSPITAL">
<td>GREEN CLINIC SURGICAL HOSPITAL</td>
<td>2701.72727272727</td>
<td>2821</td>
<td>11</td>
</tr>......</tbody></table>

But event is not firing at all

Upvotes: 2

Views: 149

Answers (3)

Fenton
Fenton

Reputation: 250922

The name attribute has been deprecated, and your selector is looking for an id, so change:

<table name="drg_table">

To

<table id="drg_table">

And use:

$("#drg_table").on("click","tr td").click(function(){     
    alert('I am here');
});

Upvotes: 2

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

drg_table is the name attribute not an id. Additionally you just used .closest(). Actually it will look for the ancestor elements, not the descendants.

Try,

$("table[name='drg_table'] tr td").click(function(){     
      alert('i am here');
});

Upvotes: 4

codingrose
codingrose

Reputation: 15699

.closest() traverses up through the DOM.

In your case, closest() can be applied to td or tr td but not on table itself.

Attribute is written in square brackets and id as #+id.

Try:

$("table[name='drg_table']").find("tr td").click(function(){     
    alert('i am here');
});

OR

$("table[name='drg_table']").on("click","tr td").click(function(){     
    alert('i am here');
});

OR

$("table[name='drg_table']tr td").click(function(){     
    alert('i am here');
});

Upvotes: 3

Related Questions