Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remembering the last value passed to a JavaScript function called on click

Below is my code fragment:

<div onclick = "myClick('value 1')">
    button 1
</div>
 <div onclick = "myClick('value 2')">
    button 2
</div>

Basically when I for each click on a different div, a different value will be passed to the JavaScript function.

My Question is how can I keep track of the value passed in the previous click?

For example, I click "button 1", and "value 1" will be passed to the function. Later, I click on "button 2", I want to be able to know whether I have clicked "button 1" before and get "value 1".

like image 316
sams5817 Avatar asked May 03 '26 20:05

sams5817


1 Answers

Just add it to a variable in your script:

var lastClicked;
var myClick = function(value) {

    lastClicked = value;

};
like image 153
DoctorLouie Avatar answered May 05 '26 08:05

DoctorLouie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!