how to put java event onclick

how to put java event onclick

kawinkawin Posts: 4Questions: 0Answers: 0
edited June 2011 in General
I just can't figure out where do i put onclick event on each data row. I would like to make a simple alert when click on each row.
for example when i use ordinary html table i can use onclick on each tr like this
[code]test[/code]

in datatables how do i achieve this.

please help

Thanks to all

Replies

  • mirkecmirkec Posts: 8Questions: 0Answers: 0
    Use unobtrusive way.

    example:
    [code]
    $('#oTable tbody tr').live('click', function () {
    //your function here
    }
    [/code]
  • GregPGregP Posts: 495Questions: 9Answers: 0
    edited June 2011
    I'm using a method like mirkec's but with the delegate function:

    [code]
    $('#divContainingMyTable').delegate('tbody tr', 'click', function() {
    //your function here
    }
    [/code]

    If #divContainingMyTable is something that is ever-changing, you could do something more dynamic using .closest() and .parent() to traverse the DOM looking for an appropriate element to attach the listener to.

    Note on using .delegate: I don't understand the technical implications of how the two methods handle bubbling differently, but I'm taking people's word for it that .delegate() is a more efficient jQuery method than .live(). If I'm being honest, I don't see any real reason to not just do it mirkec's way unless it is a design priority to meticulously squeeze every drop of performance out of your application. I present it here only as an option with no real evidence that it's better.
  • GregPGregP Posts: 495Questions: 9Answers: 0
    edited June 2011
    Edited 'cause I might have been imagining things.

    Short version: switching from .click() in fnRowCallback to .delegate() has not solved my memory leak. Not sure which thread that news is better off in. ;-)
  • kawinkawin Posts: 4Questions: 0Answers: 0
    thanks to all.
    i got it to work by using .live as mirkec advise. thank u.
    then I shall try GregP's way to see what's different.

    thanks to all again.

    you guys are so cool.
This discussion has been closed.