• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 995
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can determine an Enter pressed. Take a look here.

./pope
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
trupti nigam
Ranch Hand
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
trupti nigam
Ranch Hand
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 647
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
any answers to my question...please

Thanks,
Trupti
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you might just refer the form by document.forms[0].

Nick
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
reply
    Bookmark Topic Watch Topic
  • New Topic