register onclick problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • obarash
    New Member
    • Jan 2007
    • 25

    register onclick problem

    I ran into a strange problem:

    I'm adding and removing rows dynamicly,
    each row has it's id and remove button

    when I removing a row i go on all the next rows and update their ID
    I also need to update the delete row button.

    the problem is that the button has onclick="delete Row(rowId)"

    and when I remove a row and update all the next elements

    all of them are calling the deleteRow with the same ID (the id of last one)

    Code:
    i = currentID;
    while(i < ElementsCounter){
        newID = i - 1;
        $("delete"+i).id = "delete"+newID;
        $("delete"+newID).onclick = function(){ deleteRow(newID); };
    }
    any idea will help
  • obarash
    New Member
    • Jan 2007
    • 25

    #2
    set onclick effect other onclicks

    setting onclick of one element happans to effect other elements onclick

    I'm adding and removing rows dynamicly,
    each row has it's id and remove button.

    when I removing a row i update all the next rows IDs
    and the onclick functino to call the deleteRow(rowID ) with the new ID.

    All next elements onclick function
    is calling the deleteRow function with the same ID (the id of last iteration)

    for example:
    all of the button call deleteRow(2)


    [PHP]i = currentID;
    while(i < ElementsCounter ){
    newID = i - 1;
    document.getEle mentById("delet e"+i).id = "delete"+ne wID;
    document.getEle mentById("delet e"+newID).oncli ck = function(){ deleteRow(newID ); };
    }[/PHP]

    Thanks from advance

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Merged duplicate threads.

      How about passing "this" to the delete function instead?

      Comment

      • obarash
        New Member
        • Jan 2007
        • 25

        #4
        Originally posted by acoder
        Merged duplicate threads.

        How about passing "this" to the delete function instead?

        if I pass the obj itself how can i get the row ID?

        there is another way to add an onlick event?

        Comment

        • Eyeinstyin
          New Member
          • Sep 2007
          • 20

          #5
          Originally posted by obarash
          I ran into a strange problem:

          I'm adding and removing rows dynamicly,
          each row has it's id and remove button

          when I removing a row i go on all the next rows and update their ID
          I also need to update the delete row button.

          the problem is that the button has onclick="delete Row(rowId)"

          and when I remove a row and update all the next elements

          all of them are calling the deleteRow with the same ID (the id of last one)

          Code:
          i = currentID;
          while(i < ElementsCounter){
              newID = i - 1;
              $("delete"+i).id = "delete"+newID;
              $("delete"+newID).onclick = function(){ deleteRow(newID); };
          }
          any idea will help
          what is "$"
          and i is not getting decrementted

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by obarash
            if I pass the obj itself how can i get the row ID?

            there is another way to add an onlick event?
            If you're deleting a row, why do you need the row ID? However, if you really need it, use the rowIndex property.

            Comment

            • obarash
              New Member
              • Jan 2007
              • 25

              #7
              Originally posted by acoder
              If you're deleting a row, why do you need the row ID? However, if you really need it, use the rowIndex property.
              Thanks I'll see if I can use it

              Comment

              • obarash
                New Member
                • Jan 2007
                • 25

                #8
                Originally posted by Eyeinstyin
                what is "$"
                and i is not getting decrementted
                $ is prototype refernce.
                the implementation is very easy:

                function $(element){
                return document.getEle mentById(elemen t);
                }

                in the prototype libary there a simple validition if it is an obj

                Comment

                • obarash
                  New Member
                  • Jan 2007
                  • 25

                  #9
                  Originally posted by acoder
                  If you're deleting a row, why do you need the row ID? However, if you really need it, use the rowIndex property.
                  I can't use it my code is like this
                  <tr>
                  <td>
                  <table>
                  <tr>
                  <td>
                  <td>
                  <td>button</td>
                  <tr>
                  </table>
                  <td>
                  <tr>

                  I need it to stay as is my problem is relly in the 'this'
                  in the loop that i described before if i write this it is not a refernce to the button itself.
                  how can i pass this?

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Can you post the rest of the code (if it's not too much) or a link?

                    Comment

                    • obarash
                      New Member
                      • Jan 2007
                      • 25

                      #11
                      Originally posted by acoder
                      Can you post the rest of the code (if it's not too much) or a link?
                      i solved my problem!!

                      I've set the <td> that contatins the button only the row id and just call deleteRow()

                      in the deleteRow():
                      var obj = event.srcElemen t;

                      and then I use obj.parentNode. id

                      and in the loop i just update the <td> id.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Originally posted by obarash
                        i solved my problem!!

                        ...
                        in the deleteRow():
                        var obj = event.srcElemen t;
                        Glad you managed to solve it. Just a note though: srcElement is IE-only. You need to use target for standards-compliant browsers. See, for example, this link.

                        Comment

                        Working...