0

I have a function in html:

<script>
function update_x(obj) { 
 ...
}
</script>

and I call it on click in html with onclick="update_x(this)" (inside of <div class="aaa">).

How can be the same achieved in jquery? I've tried some stuff, like:

$('.aaa').click(update_x);
 }); 

and

$('.aaa').click(function () {
    $(this).update_x(1, false);
 }); 

neither won't work...

weaponx
  • 135
  • 5
  • 18

1 Answers1

2

This would be equivalent:

$('.aaa').click(function () {
    update_x(this);
}); 

But you don't need to use that. Just change your function to

function update_x(event_obj) { 
    // 'this' will be the clicked object automatically
    // plus, you have further info in the event object
}
$('.aaa').click(update_x);

Make sure $('.aaa').click(update_x) is called after the element with class "aaa" exists in the DOM. You can wrap that code in a document.ready handler, or use event delegation.

Community
  • 1
  • 1
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • probably you can mention about document.ready as well. Probably that could be another reason why his first snippet didn't work – PSL Sep 27 '13 at 16:35
  • 1
    Added a comment about that, but I believe his first snipped didn't work because he was trying to read `obj` instead of `this` inside the listener. – bfavaretto Sep 27 '13 at 16:38
  • That 1st solution is exactly what I need in my specific case. It can't be more simple and it works. Thanks bfavaretto :) – weaponx Sep 27 '13 at 16:39