check if an input field is selected using javascript or jquery [closed]
How can I check if an input field is selected or not, using javascript or jquery ?
Any help would be much appreciated! Thanks!
Some googleing found a quick solution.
from http://forums.digitalpoint.com/showthread.php?t=77983
There's no built-in way; you have to use the onfocus and onblur events to set some form of flag for each element to be read.
This script automates the process for any number of forms:
Code:
<script type='text/javascript' >
/* (c)2006 Stephen Chalmers
*
* Appends a 'hasFocus' flag to all text/textarea form
* elements.
*
Insert this script anywhere below the last form in the document.
To read the current focus state of a specified element, use:
if( document.forms.myForm.myElement.hasFocus )
...
***/
for(var i=0, df=document.forms, len=df.length; i<len; i++)
for(j=0, els=df[i].elements; j<els.length; j++)
if( /^text/.test( els[j].type ) )
{
els[j].hasFocus=false;
els[j].onfocus=function(){this.hasFocus=true;};
els[j].onblur =function(){this.hasFocus=false;};
}
</script>
Have a look at checked and selected
精彩评论