Solved

Which button was pressed in a form with Javascript?

Posted on 2013-11-06
3
637 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

737 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