Search code examples
javascripttouch

Event Touch the JavaScript button


good afternoon I created button to start the rotation of a motor in an ESP8266 when tight it turns when released it stops to turn. My problem is that it does not work for the cell phone because they use the Touth function instead of the mouse function. I would like the help of you, because I could not implement the touth function in the button I searched in several places but I did not find a solution.

<input type="button" onmousedown="Horario()" value="horario" class="event" onmouseup="Stop()">


<input type="button" onmousedown="AntHorario()" value="Anthorario" onmouseup="Stop()">


function Horario() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://192.168.137.55/horario=1");
xmlhttp.send();
}
function AntHorario() {

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://192.168.137.55/anthorario=1");
xmlhttp.send();
}
function Stop() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://192.168.137.55/stop=1");
xmlhttp.send();
}

Solution

  • Looks like all you need is to add the touch events (ontouchstart and ontouchend attributes):

    <input type="button" onmousedown="Horario()" value="horario" class="event" onmouseup="Stop()" ontouchstart="Horario()" ontouchend="Stop()">
    <input type="button" onmousedown="AntHorario()" value="Anthorario" onmouseup="Stop()" ontouchstart="AntHorario()" ontouchend="Stop()">
    

    You can read more about touch events here: Javascript touch events