How do I know if a button has been pressed?

In javascript, when a user clicks a button, how do you know if it was the same button that he clicked the last time? For example, if the user presses the play button and the stop button a few seconds later, how can I know that he pressed the play button before the stop button?

thank!!

+3
source share
7 answers

Enter the global link to the button that was clicked in the event handler for clicking the corresponding button and is compared with it in subsequent runs. Code example:

<script type="text/javascript">
    var prevButton = null;
    .
    .
    .
    .
    function play(el)
    {
        if(el == prevButton) //Check if User has clicked the same button
        {
            //Add your code here
        }
        else
        {
        }
        prevButton = el;
    }

    .
    .
    .
    function stop(el)
    {
        if(el == prevButton) //Check if User has clicked the same button
        {
            //Add your code here    
        }
        else
        {
        }
        prevButton = el;
    }
    .
    .
    .
</script>
<input type="button" value="Play" onClick="play(this)"/>
<input type="button" value="Stop" onClick="stop(this)"/>
0
source
var lastButton;

<input type="button" onclick="function(lastButton = this){}" />
+1
source

jquery- javascript, toggle event:

html-

<input type="button" id="mybutton">

javascript

$('#mybutton').toggle(function() {
    //here play function
}, function() {
    //here stop function
});

add remove css , .

+1

playClicked true . "".

0

, . - (, , , ..), - .

0

onclick , , , .

0

You can create an array, and in the function that processes the click of a button, you can add the identifier of the button pressed on the array.

To get the button pressed earlier, you just get access to the second last element of the array.

0
source

Source: https://habr.com/ru/post/1781900/


All Articles