Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I determine which Submit button was used in javascript?

Tags:

javascript

I have a very simple form with a name field and two submit buttons: 'change' and 'delete'. I need to do some form validation in javascript when the form is submitted so I need to know which button was clicked. If the user hits the enter key, the 'change' value is the one that makes it to the server. So really, I just need to know if the 'delete' button was clicked or not.

Can I determine which button was clicked? Or do I need to change the 'delete' button from a submit to a regular button and catch its onclick event to submit the form?

The form looks like this:

 <form action="update.php" method="post" onsubmit="return checkForm(this);">
    <input type="text" name="tagName" size="30" value="name goes here" />
    <input type="hidden" name="tagID" value="1" />
    <input type="submit" name="submit" value="Change" />
    <input type="submit" name="submit" value="Delete" />
 </form>

In the checkForm() function, form["submit"] is a node list, not a single element I can grab the value of.

like image 438
Scott Saunders Avatar asked Feb 19 '10 18:02

Scott Saunders


People also ask

Can a form have 2 submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below.

Is button submit by default?

button : The button has no default behavior, and does nothing when pressed by default. It can have client-side scripts listen to the element's events, which are triggered when the events occur.


2 Answers

Here's an unobtrusive approach using jQuery...

$(function ()
{
    // for each form on the page...
    $("form").each(function ()
    {
        var that = $(this); // define context and reference

        /* for each of the submit-inputs - in each of the forms on
           the page - assign click and keypress event */
        $("input:submit", that).bind("click keypress", function ()
        {
            // store the id of the submit-input on it's enclosing form
            that.data("callerid", this.id);
        });
    });


    // assign submit-event to all forms on the page
    $("form").submit(function ()
    {
        /* retrieve the id of the input that was clicked, stored on
           it's enclosing form */
        var callerId = $(this).data("callerid");

        // determine appropriate action(s)
        if (callerId == "delete") // do stuff...

        if (callerId == "change") // do stuff...

        /* note: you can return false to prevent the default behavior
           of the form--that is; stop the page from submitting */ 
    });
});

Note: this code is using the id-property to reference elements, so you have to update your markup. If you want me to update the code in my answer to make use of the name-attribute to determine appropriate actions, let me know.

like image 172
cllpse Avatar answered Oct 05 '22 09:10

cllpse


You could also use the onclick event in a number of different ways to address the problem.

For instance:

<input type="submit" name="submit" value="Delete" 
       onclick="return TryingToDelete();" />

In the TryingToDelete() function in JavaScript, do what you want, then return false if do not want the delete to proceed.

like image 34
Glen Little Avatar answered Oct 05 '22 08:10

Glen Little