PDA

View Full Version : How to find out which element has focus?



Tohaki
10-30-2008, 10:06 AM
Is there a quick way to find out which element has focus?

rangana
10-30-2008, 10:37 AM
<script type="text/javascript">
var elName;
window.onload=function()
{
var el = document.forms[0].elements;
for(var i=0;i<el.length;i++)
el[i].onfocus=function()
{
elName=this.getAttribute('name');
}
}
</script>
</head>
<form>
<a href="#" onclick="alert(elName?'The element having the name of \''+elName+'\' was focused.':'No element was focused.'); return false">"Which element has the focus?"</a><br>
<input name="mytxt1" type="text"><br>
<input name="mytxt2" type="text"><br>
<input name="mytxt3" type="text"><br>
<input name="mytxt4" type="text"><br>
<input name="mytxt5" type="text"><br>
</form>

Tohaki
10-30-2008, 02:00 PM
Thanks! :)