[thelist] Using variables in DOM object names

Christian Heilmann codepo8 at gmail.com
Wed Jul 13 04:26:29 CDT 2005


On 7/13/05, VOLKAN ÖZÇELİK <volkan.ozcelik at gmail.com> wrote:
> Ryan,
> 
> There is a syntax error in your eval.
> 
> var objFrom = eval("opener.formOne."+fieldnames[i]);
> var objTo = eval("opener.formTwo."+fieldnames[i]);
> objTo.value = objFrom.value;
> 
> should do the trick. Though I have not tested.
> 
> You had better use a js debugger imho.
> 
> Venkman is a good debugger for mozilla-based browsers. for msie you
> may use microsoft script debugger.
> 
> HTH,
> Volkan.
> 
> 
> 
> On 7/13/05, Ryan Rushton <web at ryanrushton.com> wrote:
> > At 11:13 PM 7/12/2005, you wrote:
> > > > Because there are many form fields to set, I would like to place them in
> > > > an array and loop over the array, but I don't know how to properly refer
> > > > to the objects when they have a variable within their name.  Here is my
> > > > NON-working code:
> > > >
> > >
> > >How about using globally used document.getElementById (which is also
> > >the standard way of doing it) ?
> > >
> > >imho, you are over-complicating the problem.
> > >Besides imho, your code should work. if I don't miss something.
> > >Can you give an URL to check?
> >
> > It's been awhile since I did much Javscript and I totally forgot about
> > getElementById.  No less, I still can't seem to make it work, even with
> > getElementById.  I have posted a simplified version at
> > http://www.ryanrushton.com/demos/master.html


Well, I don't see you using it...

function SetFields()
{
	var fieldnames = new Array("firstname","lastname","city")
	for (i=0; i<fieldnames.length; i++)
	{
		window.opener.document.getElementById(fieldnames[i]).value=document.getElementById(fieldnames[i]).value;
	}
	window.close();
}

works perfectly here.
You accessed objects of the window that are really objects of the document.

More fabulous adventures in how to reach what you want to change: 
http://www.onlinetools.org/articles/unobtrusivejavascript/chapter2.html

and eval is evil, btw:
http://blogs.msdn.com/ericlippert/archive/2003/11/01/53329.aspx


-- 
Chris Heilmann 
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/  
Binaries: http://www.onlinetools.org/


More information about the thelist mailing list