Solved

Which button was pressed in a form with Javascript?

Posted on 2013-11-06
3
623 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 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
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)

776 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