5

I have a code in which should only be executed after an onclick event. On mobile devices its fine because I use touchstart and touchend. Is there an event similar to touchend for use on a computer?

My code currently looks similar to..

document.getElementById('trigger-box').addEventListener(function(e) {
    // the event will be used later
    // my code
});

I have tried searching the internet for an answer but unfortunately couldn't find an answer. Therefore, I have posted my question here.

10
  • @FabrizioMazzoni, indeed that is the answer. May 30, 2015 at 11:51
  • I'd say Onmousedown.
    – D. Visser
    May 30, 2015 at 11:52
  • Yes. Like @FabrizioMazzoni said, uou can see a click as a sequence o mousedown and mouseup for the same element. So your expires would be connected to the mouseup.
    – m4ktub
    May 30, 2015 at 11:52
  • @FabrizioMazzoni thanks, this is exactly what i'm looking for. For some reason I read onkeydown (my mistake). Thank you very much
    – Mudassir
    May 30, 2015 at 11:55
  • 1
    @IPAddress, thats fine, you can accept his answer. But technically, it does not answer your question becuase you requested an event after the click event. His answer does not do that.
    – AmmarCSE
    May 30, 2015 at 12:03

2 Answers 2

3

onmouseup happens before the click event is executed

<button onclick="console.log(1)" onmouseup="console.log(2)">test</button>

Therefore, the only workaround I can think of is manually calling when the click event is completed

function myClick()
{
  console.log('This is in click');
  afterClick();
}

function afterClick()
{
  console.log('This is after click');
}
<button onclick="myClick()" >test</button>

2
  • hmm, it works but I really don't recommend embedded click events in HTML ... And please use console.log(<String>) for debug purposes
    – KarelG
    May 30, 2015 at 12:16
  • @KarelG, sorry if the alerts where annoying. Updated my answer to use console.log.
    – AmmarCSE
    May 30, 2015 at 12:18
2

As per comment:

document.getElementById('trigger-box').addEventListener("mouseup", function(e) {
    // the event will be used later
    // my code
});
1
  • I believe it is necessary to use "touchend" on mobile. May 27, 2020 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.