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 3 of 3
  1. #1
    Regular Coder
    Join Date
    May 2004
    Posts
    391
    Thanks
    76
    Thanked 0 Times in 0 Posts

    add text to text box that has focus

    How can I add the text below to a text box that a user has the cursor or focus in. I won't know the name of the element the user has focus in so not sure how to insert my text. I'm building a chrome extension context menu that will insert text in user focused text boxes.


    var comments = "hello test";

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    document.activeElement returns the reference to the element that has focus.

  • Users who have thanked glenngv for this post:

    nikko50 (09-26-2013)

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,333
    Thanks
    11
    Thanked 587 Times in 568 Posts
    sometimes the menu html draws focus away from the textarea/contentEditable.
    if that's the case, bind an event to each box, onfocus, which says "window.lastBox=this;".
    you can then refer to the global variable lastBox, eg lastBox.value, lastBox.scrollTop, etc in your text injection code.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    nikko50 (09-26-2013)


  •  

    Posting Permissions

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