[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

want to differentiate the button clicks from a single page using javascript.....

Posted on 2003-11-25
12
Medium Priority
?
398 Views
Last Modified: 2008-03-17
Dear experts,

I need to have many buttons in a page and thinking of doing different actions on the individual button clicks.Say for example, i have a search screen from which different criteria options are displayed.For each criteria one button is used to submit the criteria separately.i use single form and all submit buttons.But in the processing servlet i am not able to differentiate the button clicks and the corresponding parameters.i want a solution to solve this issue.somehow i felt javascript would be of my help.But i could not get the correct script for implementing this.Can any expert would heed to help me?
thanks in advance
sat
0
Comment
Question by:sathiyam
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 8

Expert Comment

by:xabi
ID: 9818040
Hi torbjornfahlen,

Here goes an example

<html>
<head>
</head>
<body>
<form action="put_here_your_action_page" method="GET" name="myform">
Field1: <input type="text" value="" name="field1">
<br><input type="button" value="Option1" onclick="this.form.action.value='option1';this.form.submit()">
<br><input type="button" value="Option2" onclick="this.form.action.value='option2';this.form.submit()">
<br><input type="button" value="Option3" onclick="this.form.action.value='option3';this.form.submit()">
<input type="hidden" name="action" value="">
</form>
</body>
</html>


xabi
0
 
LVL 8

Expert Comment

by:xabi
ID: 9818054
As you can see the info about the burron is in the field "action"

xabi
0
 
LVL 2

Expert Comment

by:DidierD
ID: 9818070
You can do it like this

<script>
  function gotoCancel(){
    document.yourForm.action="nextPage1.do";
    document.yourForm.submit();
  }

  function gotoOK(){
    document.yourForm.action="nextPage2.do";
    document.yourForm.submit();
  }
 
  function gotoSomething(param){
    document.yourForm.action="nextPage3.html?param=" + param;
    document.yourForm.submit();
  }

</script>


<input type="button onClick="goToCancel()">
<input type="button onClick="gotoOK()">
<input type="button onClick="gotoSomething(param1)">
<input type="button onClick="gotoSomething(param2)">


Greetz,
Didier
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

by:NetGroove
ID: 9818080
Or you let your buttons be Submit buttons, give them all the same name, elt say "Submit" and assign to every button a different value according to requested action.
Like this:

<form action="yourpage.jsp">
<input type=submit name="Submit" value="new">
<input type=submit name="Submit" value="mod">
<input type=submit name="Submit" value="del">
</form>

On server side you check the button like other form elements for its value to see the submitted value: Submit == "mod"

0
 
LVL 8

Expert Comment

by:xabi
ID: 9818098
NetGroove, you are right, the only problem is the "lavel" of the button ....  It must be the same as the text to be checked at the server side.

xabi
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 9818125
do not name the submit, or you may run into problems
0
 

Author Comment

by:sathiyam
ID: 9818127
hi xabi,
is there a way to use object id of an html element and identify the element and process the click?
sat
0
 
LVL 10

Expert Comment

by:NetGroove
ID: 9818159
I named it Submt.
That name provoke NO problems.

0
 
LVL 8

Expert Comment

by:xabi
ID: 9818181
This way?

<html>
<head>
<script>
function go(sName) {
  alert("You clicked " + sName + " id button");
  document.myform.action.value = sName;
  document.myform.submit();
}
</script>
</head>
<body>
<form action="put_here_your_action_page" method="GET" name="myform">
Field1: <input type="text" value="" name="field1">
<br><input id="id1" type="button" value="Click for action 1" onclick="go(this.id)">
<br><input id="id2" type="button" value="Click for action 2" onclick="go(this.id)">
<br><input id="id3" type="button" value="Click for action 3" onclick="go(this.id)">
<input type="hidden" name="action" value="">
</form>
</body>
</html>
0
 
LVL 8

Accepted Solution

by:
xabi earned 500 total points
ID: 9818207
Even better:

<html>
<head>
<script>
function go(oButton) {
  alert(oButton.form.name);
  alert("You clicked '" + oButton.id + "' id button");
  oButton.form.action.value = oButton.id;
  oButton.form.submit();
}
</script>
</head>
<body>
<form action="put_here_your_action_page" method="GET" name="myform">
Field1: <input type="text" value="" name="field1">
<br><input id="id1" type="button" value="Click for action 1" onclick="go(this)">
<br><input id="id2" type="button" value="Click for action 2" onclick="go(this)">
<br><input id="id3" type="button" value="Click for action 3" onclick="go(this)">
<input type="hidden" name="action" value="">
</form>
</body>
</html>
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10383703
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: xabi {http:#9818207}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question