Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Which button was pressed in a form with Javascript?

Posted on 2013-11-06
3
Medium Priority
?
664 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

610 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