-
Jun 14th, 2014, 02:48 AM
#1
Thread Starter
Fanatic Member
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();
}
-
Jun 14th, 2014, 03:38 AM
#2
Re: Javascript function which focus the defined control
You want to use Arguments in your function like this:
javascript Code:
function getFocus(boxID) { document.getElementById(boxID).focus(); }
To use it:
javascript Code:
<input type="text" id="mybox" /> <script> getFocus("mybox"); </script>
-
Jun 15th, 2014, 05:22 PM
#3
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?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jun 15th, 2014, 05:46 PM
#4
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?
-
Jun 15th, 2014, 06:59 PM
#5
Re: Javascript function which focus the defined control
Much easier to use the autofocus attribute.
-
Jun 15th, 2014, 07:02 PM
#6
Re: Javascript function which focus the defined control
Originally Posted by tr333
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|