Results 1 to 6 of 6

Thread: Javascript function which focus the defined control

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    Quetta-Pakistan
    Posts
    852

    Javascript function which focus the defined control

    hi,

    I am new to javascript, i wrote a function to focus a text box named 'txtuser', this function is typically work for txtuser only, i want to make a function in which i define the name of a textbox to be focused. My code is as under
    Code:
    			function gotfocus() {
    				document.getElementById("txtuname").focus();
    				}

  2. #2
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Javascript function which focus the defined control

    You want to use Arguments in your function like this:

    javascript Code:
    1. function getFocus(boxID) {
    2.      document.getElementById(boxID).focus();
    3. }

    To use it:

    javascript Code:
    1. <input type="text" id="mybox" />
    2. <script> getFocus("mybox"); </script>

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Javascript function which focus the defined control

    Never tried to put script below the input, does this work or should be the usual onclick in the input?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Javascript function which focus the defined control

    The focus script should be below the input box because the page loads from top to bottom. If you put the focus script before the textbox is created, it wont focus.

    There is no reason to create an onclick event that focuses on the box, since you already clicked on the box?

  5. #5
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: Javascript function which focus the defined control

    Much easier to use the autofocus attribute.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  6. #6
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: Javascript function which focus the defined control

    Quote Originally Posted by tr333 View Post
    Much easier to use the autofocus attribute.
    I really need to take some time to learn HTML5... Thanks for the link.

Posting Permissions

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



Click Here to Expand Forum to Full Width