PDA

View Full Version : how to set focus from variable ?



cute
05-18-2007, 05:07 AM
hi all,

In a page code like this
var f;
f =document.Form1.output1.value;
document.Form1.f.focus();

in this,the variable 'f' has the value of the 'output1' box
how do i take this as ID for focus text box.i tried the above.
the above code didn't work .
how to do this
help me plz......

codeexploiter
05-18-2007, 11:19 AM
document.Form1.elements[f].focus();

or


document.getElementById(f).focus();

mwinter
05-18-2007, 02:19 PM
document.Form1.elements[f].focus();


or



document.getElementById(f).focus();


The former is preferable, though I would write it:



function setFocus(controlName) {
var element = document.forms.Form1.elements[controlName];

if (element.focus) element.focus();
}

which would be called using:



setFocus(f);

There are browsers (at least some versions of Safari, as I recall) that don't implement the focus method.