2 button form + which button was pressed

Discussion in 'Javascript' started by Bill_W_Stephens@yahoo.com, Apr 4, 2006.

  1. Guest

    I need a form with two buttons and ability to detect which button was
    pressed. I got this code to work using a javascript global variable,
    but I know there must be a better way using DOM. Any suggestions?


    <html>
    <head>
    <script language="JavaScript" type="text/javascript">
    // global var: which button I am pressing
    var btnWhichButton;
    //========================================================
    // Check the input fields of the form
    //========================================================
    function chkMyForm() {
    alert(btnWhichButton.value);
    document.SubmitExpenseForm1.action="#";

    // If Delete Record
    if (btnWhichButton.value == "Delete Record")
    {
    document.SubmitExpenseForm1.DeleteRecordParm.value = 'Y' ;
    window.document.SubmitExpenseForm1.submit();
    return;
    }

    // Otherwise do validation
    chkTextArea();
    document.SubmitExpenseForm1.DeleteRecordParm.value = 'N' ;
    window.document.SubmitExpenseForm1.submit();
    }

    //========================================================
    // Check Text Area
    //========================================================

    function chkTextArea() {
    // additional validation...
    alert("Extra Validation done here")
    }

    </script>

    </head>
    <body>

    <form class="noLF" name="SubmitExpenseForm1"
    action="javascript:chkMyForm();" >
    <input TYPE=HIDDEN NAME="ExpenseCode" value="AUTO">
    <input TYPE=HIDDEN NAME="AcctPeriod" value="02">
    <input TYPE=HIDDEN NAME="AcctYear" value = "2006">
    <input type="hidden" name="DeleteRecordParm">

    <!-- Submit Record Button -->
    <input type="submit" NAME="Submit" value="Submit Record"
    onclick="btnWhichButton=this">

    <!-- Delete Record Button -->
    <input type="submit" NAME="DeleteRecord" value="Delete Record"
    onclick="btnWhichButton=this">

    </form>

    </BODY>
    </HTML>
     
    , Apr 4, 2006
    #1
    1. Advertising

  2. RobG Guest

    said on 04/04/2006 1:23 PM AEST:
    > I need a form with two buttons and ability to detect which button was
    > pressed. I got this code to work using a javascript global variable,
    > but I know there must be a better way using DOM. Any suggestions?


    Change your function to be a property of an object and have the button's
    click event set a property of that object rather than of the window
    (global) object.



    > <html>
    > <head>
    > <script language="JavaScript" type="text/javascript">


    The language attribute is deprecated, remove it but keep type.


    > // global var: which button I am pressing
    > var btnWhichButton;
    > //========================================================
    > // Check the input fields of the form
    > //========================================================
    > function chkMyForm() {


    Change to:


    var chkMyForm = (function()
    {
    var btnWhichButton;

    return {
    check : function(){
    alert(btnWhichButton.value);

    // Do real checking based on value of btnWhichButton
    // Essentially put body of your current function here

    },
    setWhichButton : function(btn){
    btnWhichButton = btn;
    }
    };
    })();


    Then in the form:

    <form action="" onsubmit="chkMyForm.check()" ... >

    <input type="submit" name="SubmitRecord" value="Submit Record"
    onclick="chkMyForm.setWhichButton(this)">

    <input type="submit" name="DeleteRecord" value="Delete Record"
    onclick="chkMyForm.setWhichButton(this)">

    </form>



    [...]
    >
    > <form class="noLF" name="SubmitExpenseForm1"
    > action="javascript:chkMyForm();" >


    Don't munge the action attribute, use an onsubmit attribute (see above).


    [...]

    > <!-- Submit Record Button -->
    > <input type="submit" NAME="Submit" value="Submit Record"
    > onclick="btnWhichButton=this">


    It's not good to use 'Submit' as a form control name, it can too easily
    become 'submit' and mask the form's submit() method. It's not a problem
    here, but good to avoid anyway.


    [...]


    --
    Rob
    Group FAQ: <URL:http://www.jibbering.com/FAQ>
     
    RobG, Apr 4, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?RXJpYyBOZWxzb24=?=

    Which button pressed?

    =?Utf-8?B?RXJpYyBOZWxzb24=?=, Sep 23, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    2,453
  2. Keith Patrick

    Determining which Button was pressed

    Keith Patrick, Feb 22, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    3,402
    Keith Patrick
    Feb 22, 2005
  3. Jonathan Crawford

    finding which button is pressed

    Jonathan Crawford, Jan 24, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    481
    slagomite
    Jan 24, 2006
  4. darrel
    Replies:
    19
    Views:
    600
    darrel
    Jan 30, 2006
  5. Rune
    Replies:
    6
    Views:
    1,265
    Richie Hindle
    Jan 29, 2004
Loading...

Share This Page