javascript - passing parameter problem
Posted: Fri Apr 20, 2007 1:11 pm
i have the following script:
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
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 );
}
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