mousedown and mouseup for touch screen?

Hi Experts,

Yesterday Tom Beck helped me out with this script what does what should do on a regular PC.

But would it be possible to make this work on a touch screen?

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script type='text/javascript' src='//code.jquery.com/jquery-2.1.3.js'></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<script type='text/javascript'>
//<![CDATA[ 
$(function(){
$('#Clicker').mousedown(function () {
    var txt = '*';
    $('#output').text(txt);
    timeout = setInterval(function () {
        txt += '*';
        $('#output').text(txt);
    }, 100);

    return false;
});
$('#Clicker').mouseup(function () {
    clearInterval(timeout);
    $('#output').text('');
    return false;
});
$('#Clicker').mouseout(function () {
    clearInterval(timeout);
    $('#output').text('');
    return false;
});
});//]]>  
</script>
</head>
<body>
  <button id="Clicker">Push</button>
<p id="output"></p>
</body>
</html>

Open in new window

LVL 1
SteynskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KimputerCommented:
The code works basically the same way, but you can't rely on the mousedown and mouseout feature (they're harder to trigger on either touchscreen or graphic tablet).
Rely mostly only on the mouseup event, put your important actions there.
0
Tom BeckCommented:
I'm not an expert in designing for touch screens but the touch events are separate from the mouse events. The code would look like something the following. I tested this on my iOS simulator and it seemed to work there. Of course, a touch is simulated by a mouse click in an iOS simulator so I don't know how if my testing is valid.
var pusher = document.getElementById("Clicker");
pusher.addEventListener('touchmove', function(event) {
    clearInterval(timeout);
    $('#output').text('');
}, false);
pusher.addEventListener('touchstart', function(event) {
    event.preventDefault();
    var txt = '*';
    $('#output').text(txt);
    timeout = setInterval(function () {
        txt += '*';
        $('#output').text(txt);
    }, 100);
}, false);
pusher.addEventListener('touchend', function(event) {
    clearInterval(timeout);
    $('#output').text('');
}, false);

Open in new window

jQuery version seems to work as well.
$('#Clicker').on('touchstart', function() {
    var txt = '*';
    $('#output').text(txt);
    timeout = setInterval(function () {
        txt += '*';
        $('#output').text(txt);
    }, 100);
    return false;
});
$('#Clicker').on('touchend', function () {
    clearInterval(timeout);
    $('#output').text('');
    return false;
});
$('#Clicker').on('touchmove', function () {
    clearInterval(timeout);
    $('#output').text('');
    return false;
});

Open in new window

Any reason not to combine them, cover all bases and shorten the overall code?
$(function(){
$('#Clicker').on('mousedown touchstart', function() {
    var txt = '*';
    $('#output').text(txt);
    timeout = setInterval(function () {
        txt += '*';
        $('#output').text(txt);
    }, 100);
    return false;
});
$('#Clicker').on('mouseout touchmove mouseup touchend', function () {
    clearInterval(timeout);
    $('#output').text('');
    return false;
});
});//]]> 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SteynskAuthor Commented:
Dear Tom,

Again absolutely fabulous!

Thanks a lot,

Steynsk
0
Tom BeckCommented:
You're welcome. Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.