faqts : Computers : Programming : Languages : JavaScript : Event handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

22 of 35 people (63%) answered Yes
Recently 0 of 10 people (0%) answered Yes

Entry

Accesskey of Button fires before onblur of textbox
OnBlur/OnChange of textbox is fired after AccessKey button is clicked
Fire Onblur before accessKey event

Dec 29th, 2004 05:42
Tittle Joseph,


If you have onblur written on a textbox and you have accesskey defined 
on a button, being in textbox if you click Alt+acceskey, onblur is 
fired only after button click event, which may cause sending wrong data 
to the server.
Solution:
Trap the access key and call onblur of last element forcefully.

e..g
<body onkeydown="accessKeyEvents();" >
<form >
<script language=javascript>
function accessKeyEvents()
{	
	//Trapping ALT+R, and validating if REFRESH key clicked.
	if (event.altKey == true)
	{		
		if ( event.keyCode == 82 ) //Alt+R
		{	
			try 
			{ 
				document.activeElement.onblur(); 
				document.activeElement.onchange(); 
			}
			catch(e) {  }
		}
	}
}
</script>
<input type=text name=test1 value=dummy 
onblur='this.value=this.value.toUpperCase()' />

<input type=text name=test1 value=dummyData 
onblur='this.value=this.value.toUpperCase()' />

<input type=button accesskey=r value=Refresh 
onclick=document.form1.submit() />
</form>