Edit td selector in createdCell

Edit td selector in createdCell

redsunsetredsunset Posts: 44Questions: 15Answers: 0

Hi!
I try to add a onclick event attribute to all <a> tags with the class "ocond" in a td.

so instead of giving the onClick event to the td like this:
$(td).attr('onClick', "saveToDatabase3(this,'pricechange','"+ rowData.price +"','"+ rowData.checkboxes +"')");

I try to give it to all <a> tags with class "ocond" within the td. I tried it with:
$(td + " a.ocond").attr('onClick', "saveToDatabase3(this,'pricechange','"+ rowData.price +"','"+ rowData.checkboxes +"')");

but I had no success. Thank you in advance for any help!

best greetings

This question has an accepted answers - jump to answer

Answers

  • redsunsetredsunset Posts: 44Questions: 15Answers: 0

    I even tried it with:
    $(td).children("ocond").attr('onClick', "saveToDatabase3(this,'cond','Mint','"+ rowData.checkboxes +"')");

    but no success :(

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin

    $('a.ocond', td) or $(td).children('a.ocond') are what you want to use.

    That said, I would very much recommend against using DOM0 events. Use $('#myTable').on( 'click', 'a.ocond', function () { ... } ); instead (not in the createdCell callback - at the top level).

    Allan

  • redsunsetredsunset Posts: 44Questions: 15Answers: 0
    edited December 2018

    Thank you Allen.
    I do it in the createdCell area to get the rowData values (e.g. rowData.price).

    If I do it outside of this area and inside the ready document area with a new on click event like you suggested, how could I get the specific td values of the different rowData inside the function?

    best greetings and thanks in advance for any help

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin

    Using the row().data() method:

    $('#myTable').on( 'click', 'a.ocond', function () {
      var rowData = table.row( $(this).closest('td') ).data();
      ...
    } );
    

    The advantage of that is you only have one event and don't need to worry (as much) about memory leaks.

    Allan

  • redsunsetredsunset Posts: 44Questions: 15Answers: 0
    edited December 2018

    So I would be able to get the values with

    var value = rowData.checkboxes

    ?
    Because I get: "Cannot read property 'checkboxes' of undefined"

    thank you in advance

  • allanallan Posts: 63,468Questions: 1Answers: 10,466 Site admin
    Answer ✓

    Sorry - it should be closest('tr') not td!

    http://live.datatables.net/ruxadiwa/1/edit

    Allan

  • redsunsetredsunset Posts: 44Questions: 15Answers: 0

    great! that works! thank you very much Allan!

This discussion has been closed.