which button has been pressed?

Discussion in 'JavaScript' started by Jamie18, Aug 15, 2007.

  1. #1
    i want to use javascript to figure out which submit button has been pressed, cancel or update, before i run the form validation onsubmit..

    i.e.
    html
    
    <form .. onsubmit="checkcomplete()">
      <input type="submit" value="cancel">
      <input type="submit" value="update">
      .....
    </form>
    Code (markup):
    javascript pseudo
    
    function checkcomplete
    {
    if cancel button clicked
      return false
    else
      return results of form validation tests
    }
    Code (markup):
    any way to determine which button has been pressed?
     
    Jamie18, Aug 15, 2007 IP
  2. nhl4000

    nhl4000 Well-Known Member

    Messages:
    479
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    110
    #2
    You can do the following
    <input type="submit" value="Cancel" onclick="checkcomplete('Cancel');">
    <input type="submit" value="Update" onclick="checkcomplete('Update');">
    Code (markup):
     
    nhl4000, Aug 15, 2007 IP
  3. Jamie18

    Jamie18 Peon

    Messages:
    201
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    so i would than remove the onsubmit from the form tag?

    would the result of the checkcomplete function still determine whether or not the form is submitted? or would it simply do the check than submit regardless?

    you have given me an idea though.. if the onclick of the button works before the onsubmit of the form i could call a function to set a variable to either cancel or confirm and use that in the checkcomplete function.

    anyways, thanks for the help
     
    Jamie18, Aug 16, 2007 IP