[Solved]How to make a press and hold button in HTML ?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

[Solved]How to make a press and hold button in HTML ??

I am working on A code now but incurring some problems in how to do that. https://code.sololearn.com/W1HytmSMVYu1/?ref=app

1st May 2020, 2:20 AM
Krish
Krish - avatar
5 Answers
+ 10
This can be done in JavaScript with touchstart/mousedown and touchend/mouseup events. Essentially have a function that fires when the touchstart or mousedown event fires for an element that calls your movement functions on an interval (say every 100 ms). Then use the touchend and mouseup events to cancel your interval. So basically start and stop an interval that repeatedly calls your movement functions. use touchstart and touchend for mobile devices and mousedown and mouseup for desktop compatability. You can assign both sets to do the same thing. I implemented it here: https://code.sololearn.com/Wplw93WTQ70j/?ref=app
1st May 2020, 2:35 AM
Mike Perkowski
Mike Perkowski - avatar
+ 8
Mike Perkowski Nice job with your fix for moving the box. I hope you don't mind... but I revised your code by consolidating logic and functions where there were patterns for reuse. Krish I cleaned a bit on your HTML and JS related to click events on colors and shapes controls. https://code.sololearn.com/WUVrS54WbnSg/
1st May 2020, 7:51 AM
David Carroll
David Carroll - avatar
+ 8
David Carroll looks great! Very clean and elegant.
1st May 2020, 11:05 AM
Mike Perkowski
Mike Perkowski - avatar
+ 3
Thanks a lot
1st May 2020, 3:14 AM
Krish
Krish - avatar
+ 2
Show what you have already done
1st May 2020, 2:23 AM
Solo
Solo - avatar