Skip to content Skip to sidebar Skip to footer

Two Submit Buttons, Want To Run Separate Parts Of A Script Based On Which Is Clicked

Ok so I have two submit buttons, a standard submit button, as well as a , etc, etc button. Anyway, the second button is a delete button, but both of them go to the same function, b

Solution 1:

if (document.getElementById('deletebutton').value != '' {

You forgot the closing parenthasis, that is why you are getting an error:

if (document.getElementById('deletebutton').value != '') {

Solution 2:

One thing you could consider, have both buttons have an onclick action which sets a hidden value in the form. You can then check that in your onsubmit function. Plenty of ways to do this.

Solution 3:

you could create and assign a event handler to each of the form buttons and pass the reference to the function to determine which was fired, like this for example.

<script>functionwhichButton(o){
    if(o.id == "deletebutton"){
       alert("delete was pressed");
      //document.yourFormName.submit();
    }else{
     // document.yourFormName.submit();alert("delete wasn\'t pressed");
    }

   returnfalse
  }
</script>

set up the button

 <form name="yourFormName" action="" onsubmit="return false">
    <button type="submit" name="del"id="notdeletebutton" value="'.$org.'" onclick="return whichButton(this)">Not Delete</button>
    <button type="submit" name="del"id="deletebutton" value="'.$org.'" onclick="return whichButton(this)">Delete</button>
  </form>

Post a Comment for "Two Submit Buttons, Want To Run Separate Parts Of A Script Based On Which Is Clicked"