?
Solved

Which button was pressed in a form with Javascript?

Posted on 2013-11-06
3
Medium Priority
?
673 Views
Last Modified: 2013-11-06
Hi experts!

I’ve been struggling with this issue for a while.

So I have a form like this:
http://www.w3schools.com/js/tryit.asp?filename=tryjs_form_validation

But the difference is that I have multiple buttons in my form… I will need to find out which button was pressed. How do I do this?

For example, below code has two buttons instead of one:  

<!DOCTYPE html>
<html>
<head>
<script>
function validateForm()
{
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
</script>
</head>

<body>
<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="fname">
<input name = "1" id="1" type="submit" value="Submit">
<input name = "2" id="2" type="submit" value="Submit">
</form>
</body>

</html> 

Open in new window


So how do I know which button was pressed? I need to find out so that I can do validations with Javascript for multiple buttons.

Thanks so much!
0
Comment
Question by:nvs_victor
  • 2
3 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 2000 total points
ID: 39628677
<script>
function validateForm(e)
{
    alert("Submit name: "+e)
var x=document.forms["myForm"]["fname"].value;
if (x==null || x=="")
  {
  alert("First name must be filled out");
  return false;
  }
}
</script>

<form name="myForm" action="demo_form.asp" method="post">
First name: <input type="text" name="fname">
<input name = "1" id="1" type="submit" onclick="return validateForm(1)" value="Submit">
<input name = "2" id="2" type="submit" onclick="return validateForm(2)" value="Submit">
</form>
</html>
0
 

Author Closing Comment

by:nvs_victor
ID: 39628701
Hey GaryC123!

That does it :)  

Thanks a lot!
0
 

Author Comment

by:nvs_victor
ID: 39628773
Thank you soooooooooooooooooooooooooo much! This little issue was driving me insane... I probably was missing one simple thing, and went looking for other complex solutions.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

807 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