Click to See Complete Forum and Search --> : Set Focus to button
nickat
February 14th, 2006, 11:02 AM
Hi,
Can someone show me how i can set focus to a specific button and maintain the focus on that button even if the user click a textbox(put the cursor on the textbox). IE seems to put the focus to the first button when user click the textbox.
Thanks,
PeejAvery
February 14th, 2006, 01:38 PM
IE will set the focus but you can change it with JavaScript. With JavaScript you can't deny focus but change it at an interval of 10 milliseconds or some small one such as that.
<script language="JavaScript">
window.setInterval("FORMNAME.OBJECTNAME.focus()", 10);
</script>
nickat
February 14th, 2006, 02:45 PM
Is there any way to make it seems like a concurrent focus...for example, putting the focus to a specific button while the user is typing into a certain textbox...instead forcing the focus to one control...
Dr. Script
February 14th, 2006, 03:01 PM
There is one focus. If it is in the textbox, you can't have it on the button as well. Likewise, if the focus is on the button, typing text won't go into the text box.
nickat
February 14th, 2006, 03:15 PM
the problem i have is that, i have 4 textboxes...and then i have 3 buttons...whenever the user finish typing something to any text box and hit enter, i want it to respond as if the second button is clicked...right now, everytime a user hit enter after typing something into a text box, it will respond to the first button...
how can i get around this without making the second button become the first button...the buttons i have are [Back][Next][Exit]...the Next button act as a submit button...
Thanks,
Asok
February 14th, 2006, 03:55 PM
the problem i have is that, i have 4 textboxes...and then i have 3 buttons...whenever the user finish typing something to any text box and hit enter, i want it to respond as if the second button is clicked...right now, everytime a user hit enter after typing something into a text box, it will respond to the first button...
how can i get around this without making the second button become the first button...the buttons i have are [Back][Next][Exit]...the Next button act as a submit button...
There are several ways to accomplish this.
One way (the easy way) is to encompass your text boxes and your next button in the same form and give your next button the type 'SUBMIT'. This tells the form that when enter is pressed, the submit button should fire the onSubmit event.
Example:
<form id='frmTest' action='JavaScript: doAction()' onSubmit='JavaScript: goNext()'>
<input id='txt1' type='text'>
<input id='txt2' type='text'>
<input id='txt3' type='text'>
<input id='cmdComplete' type='SUBMIT'>
</form>
Another way is to encompass each text box in its own form. If there is only one text box within a form, it reads the enter key as a submission and fires the onSubmit event.
Example:
<form id='frm1' action='JavaScript: goNext()' onSubmit='JavaScript: goNext()'>
<input id='txt1' type='text'>
</form>
<form id='frm2' action='JavaScript: goNext()' onSubmit='JavaScript: goNext()'>
<input id='txt2' type='text'>
</form>
<form id='frm3' action='JavaScript: goNext()' onSubmit='JavaScript: goNext()'>
<input id='txt3' type='text'>
</form>
<input type='button' onclick='goNext()' value='Next'>
Finally, you could add an onKeyDown event to each text box that looks for the enter key being depressed. This is a hokey approach however.
Example:
<script>
function keyIsPressed()
{
if( event.keyCode == 13 )
goNext();
}
</script>
<input type='text' id='txt1' onKeyDown='keyIsPressed()'>
<input type='text' id='txt2 onKeyDown='keyIsPressed()'>
<input type='text' id='txt3' onKeyDown='keyIsPressed()'>
<input type='button' id='cmdNext' value='Next' onClick='goNext()'>
Let me know if you have any questions.
nickat
February 14th, 2006, 04:16 PM
what happens if i have all three buttons of type SUBMIT?...do i have to change the type from SUBMIT to BUTTON for the BACK and EXIT button or can i do something else?
Asok
February 14th, 2006, 04:19 PM
If you are trying to use the FIRST solution, then you will have issues with multiple submit buttons.
Try the second solution. You don't even have to have Submit buttons that way.
Just point the onSubmit event to the method you would like to execute when the enter key is pressed.
That's really the cleanest of the solutions.
nickat
February 14th, 2006, 04:26 PM
i was thinking about the third solution...
i need everything to be in one form...everytime an Enter happen, i need to pass all the variables.
Asok
February 14th, 2006, 04:31 PM
i was thinking about the third solution...
i need everything to be in one form...everytime an Enter happen, i need to pass all the variables.
Ah. Still. Do not use multiple SUBMIT typed inputs within one form. When you click on the button(s), they will all do the same thing (whatever is in the onSubmit/action attribute of the form tag).
Good Luck!!!
nickat
February 14th, 2006, 04:36 PM
Thanks a lot for all your help Asok.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.