开发者

check if an input field is selected using javascript or jquery [closed]

开发者_运维百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜