Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Struts and the fly likes hitting the enter on the keyBoard should default to Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "hitting the enter on the keyBoard should default to "YES" button on the html/jsp page" Watch "hitting the enter on the keyBoard should default to "YES" button on the html/jsp page" New topic
Author

hitting the enter on the keyBoard should default to "YES" button on the html/jsp page

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Hello All,

I have a JSP page,on which there are two buttons. "YES" and "NO". when the user enters the valid data and hits the enter on the keyBoard, it should default to the "YES" button. Does any one knows how to achieve this?

I am using struts. So I have JSP ,an action form and and an action class.

The problem is that we have a common logic developed for all the forms in the project. We have three cases like default,buttonYes and buttonNo.And when the user hits the yes ,he is directed to that case. Due to this the enter key pressed is not gettign recognised and when the user presses enters it goes to default case.

I tries the windows.document.onKeyPress(),but hitting "enter" key is not getting recognised .can some one plese suggest something?

Thanks,
Trupti
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
I think you can determine an Enter pressed. Take a look here.

./pope


blog - InfoQ.com
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
That JavaScript resource somewhat implies this, but what happens when you press Enter varies from browser to browser and, in some cases, from one version of a browser to the next.

I've run into this in the past and it can be a real bear to get something that works for everyone in every browser. I don't know that I can give you any real sound advice, but I will say this - when you have something that you think works, try it in as many browsers as you can - it may not work in them all.


SCJP Tipline, etc.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Hello Ali Pop,

How do I transfer the captured key to my action form .

How do I pass the "Key" value to my action form?
For the above code if i press "ENTER" ,nothing happens.

Thanks,
Trupti
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
If you actually want to "click" a button via JavaScript, you can access the submit element via the form's elements array, like this:



I tested this in IE and it works great. I believe it will also work in NS6, but I haven't tested it and I'm certainly not sure about other browsers or older versions.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
hello Corey,

document.<onKeyPress< = keyhandler;

function keyhandler(e) {
if (document.layers)
Key = e.which;
else
Key = window.event.keyCode;
}
I am able to capture the enter key value.What I don't get is that How do I pass this Key value to my ActionForm. I have two Buttons "Yes" ,"No" on my JSP. and corresponding to these two buttons I have get methods like getYes(),getNo() in my actionForm. In order to recognise that the user has pressed the enter key I have to pass the captured KeyCode to my action form where there can be a method like getKey() which will get the KeyCode and then direct it to the same logic as pressing the "yes" button.
I want the "pressing Enter key" to behave exactly similar as pressing "Yes".In struts there is no name for the Forms. So I can not do like document.formName.submit.
Can you please suggests me something.

Thanks,
Trupti
[ November 05, 2004: Message edited by: trupti nigam ]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

Buttons get submitted like any other value in the form.
If you've a button named "key" with the value of "Yes", it will call
setKey("Yes") on the ActionForm.

If you want to emulate the button selection using javascript, just add the parameter so the form will add key=Yes to the post.


A good workman is known by his tools.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
Marc,

I don't have a button named "Key" with value "yes" .
What I want is when the user hits the Enter on the keyBoard,it should have the same effect as if th user has pressed the "Yes" button on the JSp.
In order to take care of the "Enter" on the keyboard,using javaScript I am capturing the keycode for the "Enter" and storing it on variable "Key."
(see my previos posts)
My question is how do I pass this key variable to my ActionForm?

Thanks,
Trupti
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
My question is how do I pass this key variable to my ActionForm?


(javascript)
//capture Enter key event
document.MyForm.buttonYes.click();//assuming buttonYes is a submit button
// note: it would be safer (in terms of cross-browserness) to go with
// getElementById

then in Action

if( request.getParameter( "buttonYes" ) != null ) {
// do buttonYes stuff
}
[ November 05, 2004: Message edited by: Ray Stojonic ]
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727


I don't have a button named "Key" with value "yes" .
What I want is when the user hits the Enter on the keyBoard,it should have the same effect as if th user has pressed the "Yes" button on the JSp.
In order to take care of the "Enter" on the keyboard,using javaScript I am capturing the keycode for the "Enter" and storing it on variable "Key."
(see my previos posts)

Sorry, my false assumption. But my explanation is still a valid one. Please don't assume that my post is void just because of that.

Have your form submit as if it were the yes button that submitted it even when Enter is pressed. It doesn't make any sense to create another "key" value if it handles the exact same way as the Yes button. Ray's example of using the click() method in javascript is better than what I suggested of slipping in a new parameter.

I'd like to add that if the user has javascript turned off, most browsers treat a click of Enter as a submission of the first submit button in the page. If that button is not the Yes button, the person will click Enter and wonder why it isn't working correctly.
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
hello Marc and ray,

In struts the form doen't have name.
So in the below line in place of myForm what do I need to put?
I have a line below in my JSP.
<html:form action="/Confirm" method="post" >
If I put only confirm in place of MyForm nothing happens.


document.MyForm.buttonYes.click();//

Thanks,
Trupti
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
any answers to my question...please

Thanks,
Trupti
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Originally posted by trupti nigam:
In struts the form doen't have name.


The name attribute for the html form tag has been deprecated and apparently no longer appears in the Struts html:form tag.

Use styleId in the tag and getElementById() in the javascript instead.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Or you might just refer the form by document.forms[0].

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5018
    
    8

Originally posted by trupti nigam:
hello Marc and ray,

In struts the form doen't have name.
So in the below line in place of myForm what do I need to put?
I have a line below in my JSP.
<html:form action="/Confirm" method="post" >
If I put only confirm in place of MyForm nothing happens.


document.MyForm.buttonYes.click();//

Thanks,
Trupti


Look at the generated HTML. html:form will generate something like this:

<form name="formAssociatedWithConfirmAction" action="/Confirm.do" method="post">

Where formAssociatedWithConfirmAction should be whatever is the "name" attribute of the "Confirm" action mapping.

You could also follow the other suggestion to use document.forms[0].

I would just do this:

document.getElementById("buttonYes").click();

If you do this, make sure you are using the "styleId" attribute value (for elements rendered with struts-html tags) or the "id" attribute value (for elements rendered with standard HTML input tags).


Junilu - [How to Ask Questions] [How to Answer Questions]
 
Consider Paul's rocket mass heater.
 
subject: hitting the enter on the keyBoard should default to "YES" button on the html/jsp page