**working version** function update(box){ if(box==1){ document.nextform.box1.value++ } if(box==2){ document.nextform.box2.value++ } } **prevferred version** function update(box){ var a1 = "document.nextform.box" var a2 = ".value" a1+box+a2 //this is the problem line } What I am trying to accomplish is the removal of the IF's statements. Any suggestions? thanks
I am not sure what you are trying to do in your example? Are you trying to dynamically generate the name of the box? if this is what you are trying to do to get rid of the if statement you need to use an eval statement to evaluate the string as an object. var myval = eval(a1+box+a2)
Me neither, but if you allow me one suggestion prefer: document.getElementById('box1').value to document.nextform.box1.value that way you can code differently using: update(i) where "i" could be 1 or 2 in your example and using the generic: document.getElementById('box'+i.toString()).value. Does it solve the problem?
Make it clear that requires the element to have an id, even thought I.E. would allow it. toString isn't needed there.
Of course it requires an id... And the toString insures standard compatibility between same types of variables. It's good pratice only.
I have attached a working example. I am trying to get rid of repeating IF statements but I don't know if there is a way or if it's possible document.nextform.box1.value++ what I am trying to do with the code above is that number 1 to be changing and the value returned to the form. I hope I am making myself clearer. thanks again I will try your suggestions and see if it works
thanks for all your help I used eval(a3) and it worked I also corrected var a2 = ".value to var a2 = ".value++