469,319 Members | 2,239 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,319 developers. It's quick & easy.

register onclick problem

25
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="deleteRow(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)

Expand|Select|Wrap|Line Numbers
  1. i = currentID;
  2. while(i < ElementsCounter){
  3.     newID = i - 1;
  4.     $("delete"+i).id = "delete"+newID;
  5.     $("delete"+newID).onclick = function(){ deleteRow(newID); };
  6. }
  7.  
any idea will help
Nov 24 '07 #1
11 1450
obarash
25
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.getElementById("delete"+i).id = "delete"+newID;
document.getElementById("delete"+newID).onclick = function(){ deleteRow(newID); };
}[/PHP]

Thanks from advance
Nov 25 '07 #2
acoder
16,027 Expert Mod 8TB
Merged duplicate threads.

How about passing "this" to the delete function instead?
Nov 26 '07 #3
obarash
25
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?
Nov 26 '07 #4
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="deleteRow(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)

Expand|Select|Wrap|Line Numbers
  1. i = currentID;
  2. while(i < ElementsCounter){
  3.     newID = i - 1;
  4.     $("delete"+i).id = "delete"+newID;
  5.     $("delete"+newID).onclick = function(){ deleteRow(newID); };
  6. }
  7.  
any idea will help
what is "$"
and i is not getting decrementted
Nov 27 '07 #5
acoder
16,027 Expert Mod 8TB
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.
Nov 27 '07 #6
obarash
25
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
Nov 27 '07 #7
obarash
25
what is "$"
and i is not getting decrementted
$ is prototype refernce.
the implementation is very easy:

function $(element){
return document.getElementById(element);
}

in the prototype libary there a simple validition if it is an obj
Nov 27 '07 #8
obarash
25
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?
Nov 27 '07 #9
acoder
16,027 Expert Mod 8TB
Can you post the rest of the code (if it's not too much) or a link?
Nov 27 '07 #10
obarash
25
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.srcElement;

and then I use obj.parentNode.id

and in the loop i just update the <td> id.
Nov 27 '07 #11
acoder
16,027 Expert Mod 8TB
i solved my problem!!

...
in the deleteRow():
var obj = event.srcElement;
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.
Nov 28 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Shock | last post: by
5 posts views Thread by moondaddy | last post: by
33 posts views Thread by Snis Pilbor | last post: by
3 posts views Thread by Michael_R_Banks | last post: by
3 posts views Thread by shapper | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.