0

So, I've already tried the selected answer on this:

How to disable ondblclick in JavaScript?

but it doesn't work, on my end. As per usual, I'm suspecting it has something to do with IE8 (since a lot of my previous problems were related to IE8 issues).

This is how my button looks like, keyBtnEvent is a function that changes the class of the div:

function keyBtnEvent(key, id, event) {
  //change class of object with id = 'id'
  console.log(event + 'event of keyBtnEvent called');
}
<div id="2Key" class="key"
  onmouseup="keyBtnEvent('2','2Key','up')"
  onmousedown="keyBtnEvent('2','2Key','down')">
  <button>2</button>
</div>

So, how do i disable ondblclick in IE8, without using jquery?

Aj Godinez
  • 115
  • 1
  • 11
  • 1
    Please [edit] your question to show the `keyBtnEvent()` function. Based on the code you have shown, it should possibly (probably) be `onmouseXYZ="return keyBtnEvent(...)"` (that is, return the value from the function, assuming it returns `false` when you want to cancel the event). – nnnnnn May 30 '17 at 03:19
  • are you saying that i should add a `ondblclick="return keyBtnEvent('2', '2key', 'dblclick')"`?? – Aj Godinez May 30 '17 at 03:43
  • Oh, sorry, I didn't notice that your existing events didn't include `ondblclick`. Just adding an `ondblclick="return false"` attribute should do it. (Not that I normally recommend inline event attributes, but since you are already using them for the other events.) – nnnnnn May 30 '17 at 03:45
  • 1
    Can you please clearly state what it is you expect here? Even with double click disabled, single clicks will still happen. – Scott Marcus May 30 '17 at 04:25

1 Answers1

0

This is the proper IE 8 way to do it:

var key = document.getElementById('2Key')

// Feature detection for DOM Event Standard and IE 8 and less
if(window.addEventListener){
  // W3C DOM Event Standard
  key.addEventListener("dblclick", function(evt) { 
    evt.preventDefault();    // stop the event
    evt.stopPropagation();  // don't bubble the event
  });
} else if(window.attachEvent){
  // IE 8 or less
  key.attachEvent("ondblclick", function(evt) { 
    evt = evt || window.event;
    evt.returnValue = false;  // stop the event
    evt.cancelBubble = true;  // don't bubble the event
  });
}

Also, you should not be using inline HTML event attributes (onmouseover, onmouseout, etc.). Here's why. Instead, you should be doing all your JavaScript work in a dedicated script and use .addEventListener() (or the above attachEvent() method for IE 8 or less).

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
  • this does not work for me... i knew that dblclick is still not disabled because the button class doesn't change on a dblclick (when it's supposed to - i.e. mousedown changes the button class, but only does so for the first click) – Aj Godinez May 30 '17 at 03:49
  • i commented before your edit. so uhm... yeah, let me try it out first. – Aj Godinez May 30 '17 at 03:57
  • I am not sure what you mean when you say that *the button class doesn't change on a dblclick* - that's the whole point - to disable the double click event. Here is a working Fiddle with a simulated `keyBtnEvent` function. https://jsfiddle.net/11Ln4ago/9/ – Scott Marcus May 30 '17 at 04:11
  • @RobG IE 8 does not support `.addEventListener()`. It wasn't added until IE 9. And, while certainly not a bad idea to test against the object being called, with `.addEventListener()` it doesn't matter because it was implemented across the board, not object by object. See compatibility at bottom of: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener – Scott Marcus May 30 '17 at 04:19
  • There you go, had the wrong version loaded. ;-) But I also thought that *addEventListener* was supported in IE 8, confirmation bias I guess, should have trusted [*MDN*](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener). In any case, this isn't the OP's issue. – RobG May 30 '17 at 04:26