SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    See if an input has focus?

    Is there a way with javascript to see if a form input has focus? I know I can use onblur and onfocus events and keep track of it with a variable, but those are not as reliable.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,147
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    focus

    I had trouble determining select option focus, but with other inputs something like this should work
    if(document.formid.inputid.focus == true)//do stuff

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I always thought using a method without the brackets returned true if the function was availible and false if not. I will give it a try.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,147
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    brackets

    If it's only 1 statement you can leave the brackets out. It's more of a preference. Using them doesn't hurt if you're more comfortable reading your script with them. These are equivalent.
    PHP Code:
    if(== 1) echo "hey"
    PHP Code:
    if(== 1){
    echo 
    "hey";


  5. #5
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    focus() is a method to give an element focus. You can't use that for this.

    IE has the activeElement property for this, but other browsers don't have a similar property.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,147
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)

    focus

    @Kravvitz, right you are. And I would've bet it was a property and a method. I guess using an event handler is the way to go, reliable or not.

  7. #7
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague
    If it's only 1 statement you can leave the brackets out. It's more of a preference. Using them doesn't hurt if you're more comfortable reading your script with them. These are equivalent.
    My mistake, I meant parentheses (). If you leave the parentheses off of a function in JavaScript, it returns 'undefined' if the function is not defined, or something like this: 'function focus() { [native code] }'. See quriksmode for a good article on this.

    I guess I will resort to using the events.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •