Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Focus on InputText OnChange

Status
Not open for further replies.

lmmoreira

Programmer
Jun 27, 2008
8
0
0
BR
Hi,

There are some validations on my JavaScript code I prefer to do on input-text's "onchange";

The validation is ok, but when something is wrong I run an alert and should turn the focus for this.focus();

But, the this.focus() doesn't work and the validated control lost the focus;

Like

txtEmail.onchange = function(){
if(this.value == ""){
alert("Please, type the e-mail");
// the code below doesn't work;
this.focus();
}
}

Does anyone know how to make it happen?

Thank You










 
Sorry I Can't, but on IE 7 and FF3 it doesn't work..

but, the code is as simple as the code I posted
 
wow. i hadnt noticed this bug before but very interesting.

this seems to work for some reason

Code:
<input type="text" name="txtEmail" id="txtEmail" value="bbb" />
 
<script type="text/javascript"> 
<!--
document.getElementById('txtEmail').onchange = function(){       
	if(this.value == "a"){
		alert("Please, type the e-mail");
		setTimeout(fieldFocus,100);
	}
}
function fieldFocus(){
	document.getElementById('txtEmail').focus();
}
//-->
</script>

I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top