-2

Please bear my ignorance.

I have a page with one button on it:

How to detect if the user clicks (on a computer) or touches (smarthone, tablet) this button?

I read the answers of this quite similar question but the solutions involves using advanced and comlicated libraries. I am just a beginner: d you know how to do this in simle JavaScrit or jQuery ?

Determine and bind click or "touch" event

Thank you all.

Community
  • 1
  • 1

3 Answers3

0

JAVASCRIPT

With your button use

<button onclick="myFunction()">Click me</button>

Then write your myFunction() in <script> in javascript. like this

<script>
function myFunction()
{
alert('Button clicked');
}
<script>

Read more here

http://www.w3schools.com/jsref/event_onclick.asp

JQUERY

Inlcude the jquery.js in your file then give button an ID then use a script.

Button:

<button id="submitbutton">Submit</button>

Jquery

$( "#submitbutton" ).click(function() {
  alert( "Submit button clicked!." );
});

TOUCH OR CLICK JQUERY

But from what I know, the problem you can face here is the touch. Here is a code that could make for touch or click

$( document ).ready(function() {
    $('#submitbutton').on('click touchstart', function() {
      alert( "Submit button clicked!." );
    });
});

Jsfiddle

https://jsfiddle.net/0xdLjvtu/

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Lorenzo
  • 574
  • 5
  • 13
0

you could try to use a variable for this if you would like, maybe try

<script>

    var buttonTouched = 0;

    function anyFunc01() {
       buttonTouched = 1;
    }

    function anyFunc02() {
       if(buttonTouched == 1) {
          console.log("button01 has been clicked at some point");
       }
    }

</script>

<button onclick="anyFunc01()">button01</button>
<button onclick="anyFunc02()">button02</button>
TheLovelySausage
  • 3,838
  • 15
  • 56
  • 106
-1

use jQuery! var deviceEvent = ((document.ontouchstart!==null)?'click':'touchstart'); $("#mylink").on(deviceEvent ,function(){alert('click true!');});

scai
  • 49
  • 1