Page 1 of 1

javascript - passing parameter problem

Posted: Fri Apr 20, 2007 1:11 pm
by slash_gnr3k
i have the following script:

Code: Select all

var iteration = 0;

		function AddToIngList (form, iteration)
		{

			var it = iteration;

			var Ingredient = form.ingredienttoadd.value;
			form.ingredients_added.value += Ingredient + "\n";

			var objHTML, objText, objinput, objLink;

			objHTML = document.createElement('P');
			objHTML.setAttribute('NAME', 'txtTest'+1);
			objHTML.setAttribute('ID', 'txtTest'+1);
   
			var strTest = form.ingredienttoadd.value;
  			objText = document.createTextNode(strTest);

			objinput = document.createElement
('INPUT');		
			objinput.setAttribute('TYPE', 'text');
			objinput.setAttribute('NAME', 'input'+it);
			objinput.setAttribute('ID', 'input'+it);



			objLink = document.createElement('A');
			objLink.setAttribute('HREF', '#');
			objLink.setAttribute('NAME', it);
			var foo = objLink.getAttribute('NAME');
			var strOnClick = 'Test(foo);';
			objLink.setAttribute('onclick', strOnClick);
			objLink.appendChild(document.createTextNode('Delete This'));

			

			form.appendChild(objHTML);		
			objHTML.appendChild(objText);
			objHTML.appendChild(objinput);
			objHTML.appendChild(objLink);
		
		}

		function Test
(foo)
		{
			var d = document.getElementById('input'+foo);
			d.parentNode.removeChild( d );

			var e = document.getElementById('txtTest'+foo);
			e.parentNode.removeChild( e );

		}

		
which is run on the click of a form button.

basically when a user clicks a button, form.ingredienttoadd.value is dispayed followed by an input box and a <a> tag. on each click of the button var is updated, if the user clicks the button once TxtTest1 is displayed then a box called input 1 then a <a> with the name 1. on the second click the txtest is txtest2, the box is input2 etc.....

when i click the link i get the error that foo is not defined. if i alert foo on the line under where it is defined it alerts the correct number. however if i alert foo in the Test function i get undefined which means there is a problem with the variable pass
can any1 help?
thanks

Posted: Fri Apr 20, 2007 1:46 pm
by Weirdan
Your onclick handler loosing the scope because it's specified as a string. Use closure instead:

Code: Select all

// instead of
var strOnClick = 'Test(foo);';
objLink.setAttribute('onclick', strOnClick); 

// do this
objLink.onclick = function() { Test(foo); };

Re: javascript - passing parameter problem

Posted: Fri Apr 20, 2007 1:47 pm
by Weirdan
slash_gnr3k wrote: can any1 help?
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.

Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.

Posted: Mon Apr 23, 2007 11:49 am
by slash_gnr3k
thanks very much - that sorted it!