1

I have added following table dynamically using Javascript:

    <table class="ActionMenu">
<tr>
<td>
<p>Ghanshyam</p>
</td>
</tr>
</table>

i want to get alert on Click of this table..

I tried:

$('body').on("click", ".ActionMenu tr", function (e) {
      alert("abcd");
 });

$('.ActionMenu ').on("click", "tr", function(){
   //do something
});

$('.ActionMenu').live('click',function(){});

$('#ActionTab').delegate('.someClass','click',function(){});

but none of the following Method work.. how can i achive my goal? Thanks

7
  • Perhaps a jsfiddle would be helpful in this case, if my answer doesn't help Commented May 16, 2012 at 10:59
  • the first .on statement should work fine. Maybe the problem is elsewhere Commented May 16, 2012 at 10:59
  • $('.ActionMenu').live('click',function(){}); works: jsfiddle.net/QMNcc. Maybe you could reproduce your bug in a JsFiddle Commented May 16, 2012 at 11:01
  • I am deploying it on Server side, and it is not working Commented May 16, 2012 at 11:03
  • 1
    @EliasVanOotegem I think you mean depricated - it still works and can still be used. In JQuery 1.7 the live method just calls the on method itself (making it backward compatable). There is no mention of the version of JQuery being used, which could be why on is not working. Commented May 16, 2012 at 11:18

3 Answers 3

2

are you enclosed your code in $(document).ready() block ? if not then try to enclose inside document.ready function

$(function(){
$('body').on("click", ".ActionMenu tr", function (e) {
      alert("abcd");
 });

$('.ActionMenu ').on("click", "tr", function(){
   //do something
});

$('.ActionMenu').live('click',function(){});

$('#ActionTab').delegate('.someClass','click',function(){});
});
Sign up to request clarification or add additional context in comments.

Comments

1

Your selectors are a tad off, at first glance, try:

$('table.ActionMenu tr').on('click',callback);

Since this isn't working: a couple of things have been suggested:

  • What version of jQuery are you using, on might not be working if you're on an old version
  • have you tried $(body).on('click', '.ActionMenu tr', function() { alert('clicked'); });? Replacing .on with .live if needed.
  • A classic problem, but easily forgotten: have you checked for typo's in either class names, id's... on both sides (server & jQuery)? --You never know
  • Is all code wrapped in a $(document).ready(function(){/*script goes here...*/});
  • Check you console for possible syntax errors or other messages

4 Comments

but this won't work on tables injected later via ajax/javascript
Yes, it will... .on is incorporates the same functionality as .live, so the event handler will be bound to all dynamically generated elements, too
Thanks for you answer but where can i put my Content Code in your above method?
the callback can be any function reference, either previously declared, but feel free to replace that with function(){alert($(this).html());} or whatever you need
1
$(document.body).on('click', '.ActionMenu tr', function() { alert('clicked'); });

See http://jsfiddle.net/MWvMH/1/

Comments

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.