Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: Find Cursor

  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Find Cursor

    Is there anyway to find which form object the cursor is in? I guess that might be referred to as having focus. I read that there is a hasFocus() method, but the only way that I can think of to accomplish this is to walk through every form element in the page and check for focus.
    Last edited by Antoniohawk; 01-16-2005 at 12:44 AM.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    onfocus().... ???

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    This is going to be used for a favelet, so I don't have access to the html and can't apply attributes like that unless I do so with javascript.

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    codegoboom@yahoo.com
    Posts
    999
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internet Explorer

    Code:
    var isFocused;
    document.onfocusin = function(){isFocused = event.srcElement;}
    *this message will self destruct in n-seconds*

  • #5
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    From that IE icon, I take it that it won't work in Firefox? I really need to find a way for it to work in gecko based browsers because I don't use IE.

  • #6
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    http://www.w3.org/TR/2000/REC-DOM-Le...ngs-htmlevents

    The focus event does not bubble, so you have to capture it.

    window.focusedElement = null;
    document.addEventListener("focus", function(event) { window.focusedElement = event.target }, true);

    Something like that I imagine would be sufficient for Gecko.

  • #7
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Thanks Jason, that's exactly what I was looking for. Now I'm having a problem with Firefox telling me that getAttribute is not a function. Here's what I have so far.
    Code:
    window.focusedElement = null;
    document.addEventListener("focus", function(event) { if (event.target.getAttribute('type') == 'text'){ window.focusedElement = event.target }}, true);
    
    function insertChar(obj) {
      alert(obj.value);
      alert(window.focusedElement);
      alert(window.focusedElement.value);
      window.focusedElement.value = obj.value;
      window.focusedElement = null;
    }
    
    <form>
    <ul id="currency">
      <li><input type="button" name="pound" value="£" title="Pound" onclick="insertChar(this);" /></li>
      <li><input type="button" name="yen" value="¥" title="Yen" onclick="insertChar(this);" /></li>
      <li><input type="button" name="euro" value="€" title="Euro" onclick="insertChar(this);" /></li>
      <li><input type="button" name="cent" value="¢" title="Cent" onclick="insertChar(this);" /></li>
      <li><input type="text" value="woot" /></li>
      <li><textarea rows="2" cols="20">The cat was playing in the garden. Suddenly a dog showed up.....</textarea></li>
    </ul>
    </form>
    It does work, but I don't understand why it throws the error. Also, as soon as I try to add in support for textareas, it kind of freaks out and doesn't seem to think that they have anything in them.
    Code:
    document.addEventListener("focus", function(event) { if (event.target.getAttribute('type') == 'text' || event.target.tagName = 'textarea'){ window.focusedElement = event.target }}, true);
    Last edited by Antoniohawk; 01-16-2005 at 05:37 AM.

  • #8
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Check event.target; it may very well be the textnode child instead of the actual form element. In the function:

    if (event.target.nodeType == 3) {
    // you really want event.target.parentNode
    }

  • #9
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Aha! Thanks for the suggestion Jason, but as it turned out, the problem was that the tag name of textarea had to be TEXTAREA when I used the tagName method.

    Edit: [Here's] the almost completed version of this script. Any suggestions?

    I just noticed that the characters have gone whacky. How can I use unicode characters correctly?
    Last edited by Antoniohawk; 01-16-2005 at 07:48 PM.


  •  

    Posting Permissions

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