Virtual Forms and Return key

Hi Everybody,

I am relatively new to JSP/JSF and started writing my very first project.

At the moment I am trying to solve a, hopefully, relatively simple problem:

I have a GUI with several text fields and a 'search' button for each text field. The idea is to let the user type a string and/or a number and then use the search button to get the information from a underlying database. The problem arise if the user hits the return button. Should that be the case, the browser seems to use the first button in the page or the last one used.

Is there a way to control which action the return key triggers?

Many thanks

Regards

Luca

[679 byte] By [SPITUa] at [2007-11-26 13:36:44]
# 1

http://onesearch.sun.com/search/onesearch/index.jsp?qt=enter+key&col=develop er-forums&subCat=siteforumid%3Ajava881&site=dev&dftab=siteforumid%3A java881&chooseCat=javaall&cs=false&rt=true

The first four results are all pertinent. If you have a look further down you'll even find one from craigmcc circa 2004. And of course Jetsons has already helped out someone with this problem.

Seems like folks have had sucess with a) arranging button as first on page or b) using javascript to intercept the enter key.

yossariana at 2007-7-7 22:22:34 > top of Java-index,Development Tools,Java Tools...
# 2
I have been trying using the followingif (event.keycode == 13) {this.form1.btnSearch.click();}in the onkeypress event for the textfield but it does not work.Am I missing something out here?ThanksLuca
SPITUa at 2007-7-7 22:22:34 > top of Java-index,Development Tools,Java Tools...
# 3

if (event.keyCode == 13) {alert("intercept");document.getElementById("form1:button1").click();}

Note camelCase on keyCode.

I'm not sure how this.form1.buttonxxx.click() works so I used getElementById()

You probably need to stop the enter key feeding through to the browser. You'll have to research how.

yossariana at 2007-7-7 22:22:34 > top of Java-index,Development Tools,Java Tools...
# 4

I have used your version, slightly modified, and it worked. I added a 'return false' at the end to try to stop the feed through of the enter key.

if (event.keyCode == 13) {alert("intercept");document.getElementById("form1:button1").click(); return false}

Any idea why using a direct id for the button does not work? It would be nice to understand that behavoiur.

Many thanks

Regards

Luca

SPITUa at 2007-7-7 22:22:34 > top of Java-index,Development Tools,Java Tools...
# 5
Sorry I'm a bit of a javascript newbie so no idea why (or even if) the direct access to the button works. I just know that I've used getElementById() before and that works.
yossariana at 2007-7-7 22:22:34 > top of Java-index,Development Tools,Java Tools...
# 6
Thanks anyway.If I manage to find out why I ll let you know.
SPITUa at 2007-7-7 22:22:34 > top of Java-index,Development Tools,Java Tools...