Zaloguj | Zarejestruj | FAQ
Anonymous

How to check which button is clicked from multiple button

+ Odpowiedz

Posty: 6 Strona 1 z 1


How to check which button is clicked from multiple button

przez akvUsername » Pn maja 21, 2012 5:14 am

Hi,

In my html, I have a form with 3 submit buttons.
I like to check which button has been clicked everytime user click.
Because I need to run some procedure in each buttons when the form is submit.

This is my code:
Kod: Zaznacz cały
<html>
   <head>
      <script language="javascript">
         function alerts(msg) {
            alert("Button clicked: " + msg);
         }
         
         function checkBtn() {
            if (document.getElementById("save").value != "") {
               alerts("Save");
            } else if (document.getElementById("update").value != "") {
               alerts("Update");
            } else if (document.getElementById("delete").value != "") {
               alerts("Delete");
            }
            return true;
         }
      </script>
   </head>
   <body>
      <br /><br />
      <form action="multiSubmit.php" method="post" onSubmit="return checkBtn();">
         <input type="submit" name="save" id="save" value="Save" /><br />
         <input type="submit" name="update" id="update" value="Update" /><br />
         <input type="submit" name="delete" id="delete" value="Delete" /><br />
      </form>
   </body>
</html>


What happen here is:
javascript ALWAYS says "Button clicked: Save" even though user has click different button.
It seems like the javascript always received all button's value even though its not been clicked.
If I add php code and check the button using isset(), it works well.

I dont know if my code is wrong because I'm very new in javascript.

So, my question is:
How to check which submit button has been clicked using javascript?
Avatar użytkownika

akvUsername

  • Posty: 3
  • Dołączył(a): Pn maja 21, 2012 3:36 am

Re: How to check which button is clicked from multiple butto

przez XainPro » Pn maja 21, 2012 6:10 am

its easy you can code like i did
Kod: Zaznacz cały
<html>
   <head>
      <script language="javascript">         
         function checkBtn(event) {
               alert(event.target.name);
         }
      </script>


   </head>
   <body>
      <br /><br />
      <form action="multiSubmit.php" method="post" onSubmit="return false;">
         <input type="submit" name="save" id="save" value="Save" onClick="checkBtn(event)" /><br />
         <input type="submit" name="update" id="update" value="Update" onClick="checkBtn(event)" /><br />
         <input type="submit" name="delete" id="delete" value="Delete" onClick="checkBtn(event)" /><br />
      </form>
     
   </body>
</html>
Avatar użytkownika

XainPro

  • Posty: 3933
  • Dołączył(a): Pt lut 17, 2012 8:10 pm

Re: How to check which button is clicked from multiple butto

przez akvUsername » Pn maja 21, 2012 6:41 am

Thank you XainPro..
Problem solved.

Anyone got another idea?
Avatar użytkownika

akvUsername

  • Posty: 3
  • Dołączył(a): Pn maja 21, 2012 3:36 am

Re: How to check which button is clicked from multiple butto

przez XainPro » Pn maja 21, 2012 3:50 pm

Mention not...
Avatar użytkownika

XainPro

  • Posty: 3933
  • Dołączył(a): Pt lut 17, 2012 8:10 pm

Re: How to check which button is clicked from multiple butto

przez soccerwebsitemaker » Cz cze 07, 2012 1:41 am

What do u mean 'other idea'?

I can give ideas for sites... i guess...
Avatar użytkownika

soccerwebsitemaker

  • Posty: 138
  • Dołączył(a): So gru 31, 2011 6:25 pm
--
Coins. Profile. Games. Chat. Fun.

CoinAwards.

Check it out at http://www.coinawards.net63.net/

Re: How to check which button is clicked from multiple butto

przez ari » N lip 08, 2012 8:57 pm

you could use jQuery and classes
Avatar użytkownika

ari

  • Posty: 15
  • Dołączył(a): Pt kwi 06, 2012 5:59 pm


+ Odpowiedz

Strona 1 z 1