I have some elements in my webpage that are clickable. When you click on them they, let's say, change their background color. Every time you click they get a different color. They are initially gray, then blue, then yellow then orange and back to gray and so on...
Now the problem is that all of this is implemented on single clicks, but I would like my double clicks to act as two single clicks and not fire a double click event (which does select the word etc...)
Let's imagine we have the following sentence
<p>This is my <span id="elem_1" className="color_1"> element </span></p>
And this is my js code:
var totalNbOfClicks = 0
var elem document.getElementbyId("elem_1")
elem.onclick = function () {
totalNbOfClicks +=1
totalNbOfClicks = totalNbOfClicks%4
elem.className = "elem_"+totalNbOfClicks
}
elem.ondblclick = function(evt) {
ClearSelection();
evt.preventDefault()
}
function ClearSelection() {
if (window.getSelection)
window.getSelection().removeAllRanges();
else if (document.selection)
document.selection.empty();
}
Now I use the ClearSelection function detailed in this answer on my double click.
I would like to somehow find a way to fire 2 single click events on a double click, but I haven't found anything that could help on SO from already asked questions.
Edit: thanks to @VLAZ's comment, I have been able to find that the problem could originate from this extra thing:
I have some extra thing that I want to do when the text get selected so I have added a
elem.onMouseUp(e){
//Do some stuff here with the selection e.g.,
var sel = window.getSelection()
selectedText = sel.toString();
var range = window.getSelection().getRangeAt(0);
console.log(selectedText)
console.dir(range)
}
Now if I had e.preventDefault()
to elem.onMouseUp(e)
, I get the intended behaviour, but that's not an option for me.