Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Sep 2010
    Thanked 6 Times in 6 Posts

    How to check if a button element was clicked?

    I know about document.getElementById('element').onclick but I was wondering about it because of the event listeners (addEventListener()). Wouldn't onclick be the old handler? Is there any way to check if a button was clicked? It would be helpful given I "need" it for a conditional.
    Coding is a challenge, get used to it
    Always remember to debug
    Try the guess & check method
    Break it down into simple steps

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by elitis View Post
    Is there any way to check if a button was clicked?

    <script type="text/javascript">
    var buttn;
    function whichBtn(frm) {
    alert (buttn);
    return false;
    <form id = "myform" >
    <input type="button" value = "Administrator" name = "Administrator" onclick="buttn=this.name; return whichBtn(this)">&nbsp;&nbsp;
    <input type="button" value = "Normal User" name = "Normal User" onclick="buttn=this.name; return whichBtn(this)">
    This knee problem is an utter pain in the backside. - Cricket player
    Last edited by Philip M; 01-19-2013 at 08:05 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts