javascript - passing parameter problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

javascript - passing parameter problem

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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); };
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: javascript - passing parameter problem

Post 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.
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

Post by slash_gnr3k »

thanks very much - that sorted it!
Post Reply