Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arguments dynamically to onclick event / Scope of variables 1

Status
Not open for further replies.

Csanad

Programmer
Jul 25, 2007
23
0
0
US
Hi,

I have the following function. Everything works except the assignment of the onclick event. I need to pass 3 arguments dynamically to the function call like this: rmdep(this, i, d). How do I go about this?

Thanks for your time,

Csanád

Code:
function rmdep(what,i,d)
{ // i is the index of current Item
  // d is the index of current Dependant Item
  var deps=document.getElementById('dep_'+i).value; // deps is the number of Dependant Items for current Item
  if (d==deps) // remove the last Dependant Item
  {
	deps--;
	document.getElementById('dep_'+i).value=deps;
  }
  else // remove Dependant Item from the middle of list; renumber following Dependant Items 
  {	
	var q=d+1;
	var depup;
	for (q; q<=deps; q++)
	{
		depup=document.getElementById(i+'_'+q);
		depup.setAttribute('name','f'+i+'_'+d);
		depup.setAttribute('id',i+'_'+d);
		depup.previousSibling.previousSibling.firstChild.nodeValue='Dependant Item ['+d+']';
		depup.nextSibling.nextSibling.[b]onclick='rmdep(this,i,d); return false'[/b];
		depup.nextSibling.nextSibling.setAttribute('id','r_'+i+'_'+d);
		d++;
	}
	deps--;
	document.getElementById('dep_'+i).value=deps; 
  }	
  while (what && what.tagName!='DIV') { what=what.parentNode; }
  if (what) what.parentNode.removeChild(what);
}
 
>depup.nextSibling.nextSibling.onclick='rmdep(this,i,d); return false';
[tt]depup.nextSibling.nextSibling.onclick=Function("",'rmdep(this,'+i+','+d+'); return false;')[/tt]
 
Thanks Tsuji! Your help is greatly appreciated, but something is still not quite right with my code and it's driving me crazy! :)

You can check it out here:


Please do the following:

- add 4 Dependant Items,
- remove the second Dependant Item (the idea here is that Dependant Items [3] and [4] get renumbered to [2] and [3], respectively)
- try removing the very first Dependant Item.

This is where the code crashes. I believe the trouble is coming from the "this" keyword's reassignment, but I'm not sure.

Any ideas?

Thanks very much!!

Csanád
 
I'd tried it out with the sequence of actions suggested, it works as expected. I cannot reproduce the problem mentioned. Before changing the scipt-line using your original line, with similar sequence of operations (adding one more operation: namely removing 2 times the item[1], it malfunctions) at certain moment. But with the revised script-line, it seems to function properly ie/moz. Did you find otherwise?
 
Yes, indeed the problem is gone. :) I must have not looked carefully. Anyway, thank you so much for your help Tsuji!

All the best!

Cheers,

Csanád
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top