input element lose focus on page load in IE

B

boldt.lars

This code works fine in Firefox and Opera, I can see that it put focus
on the correct input in IE, but by the time the page has loaded
completely it loses focus.

The first time you enter the page it works fine in IE too, but if you
click a link which gives the input a value after reloading the page the
above happens.

function setFocusOnForm() {
if (objectExists(document.formSystem)){
var elements =
document.formSystem.getElementsByTagName('input','textarea');
for (var x=0;x<elements.length;x++){
if (elements[x].tagName.toLowerCase() == 'textarea' ||
elements[x].tagName.toLowerCase() == 'input' &&
elements[x].type.toLowerCase() == 'text'){
elements[x].focus();
break;
}
}
}
}

function objectExists(obj) {
if (obj == null || obj == undefined){
return false;
} else {
return true;
}
}


in the body tag I run onload="setFocusOnForm()".

Anyone know why this happens?
 
A

ASM

(e-mail address removed) a écrit :
This code works fine in Firefox and Opera, I can see that it put focus
on the correct input in IE, but by the time the page has loaded
completely it loses focus.

The first time you enter the page it works fine in IE too, but if you
click a link which gives the input a value after reloading the page the
above happens.

function setFocusOnForm() {
if (objectExists(document.formSystem)){
var elements =
document.formSystem.getElementsByTagName('input','textarea');
for (var x=0;x<elements.length;x++){
if (elements[x].tagName.toLowerCase() == 'textarea' ||
elements[x].tagName.toLowerCase() == 'input' &&
elements[x].type.toLowerCase() == 'text'){
elements[x].focus();

here try adding :
elements[x].select();
also try :
return;
instead of :
break;
}
}
}
}

function objectExists(obj) {

return (obj == null || obj == undefined);
}
if (obj == null || obj == undefined){
return false;
} else {
return true;
}
}


in the body tag I run onload="setFocusOnForm()".

Anyone know why this happens?

IE accepts multi focus.
 
B

boldt.lars

elements[x].select() successfully selects the input value text, but I
do not want this happening.

Still, after implementing the suggested changes, it doesn't work. It
*may* look like it is the submit button who automatically receives
focus.

I just don't understand why it works the first time the page is loaded,
when the input boxes are empty, but the second time the page loads
where the input boxes has a value focus is removed from the input.
 
A

ASM

(e-mail address removed) a écrit :
elements[x].select() successfully selects the input value text, but I
do not want this happening.

Still, after implementing the suggested changes, it doesn't work. It
*may* look like it is the submit button who automatically receives
focus.

return false; ... ?
I just don't understand why it works the first time the page is loaded,
when the input boxes are empty, but the second time the page loads
where the input boxes has a value focus is removed from the input.

elements[x].focus();
elements[x].select();

or
elements[x].select();
elements[x].focus();

perhaps ?
(I haven't IE Win nor your file)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top