Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <button id="button">Custom Event preventDefault</button>
  <div id="target"></div>
  <script>
    button.addEventListener('click', checkPrevent);
    target.addEventListener('foo', function(e) {
      e.preventDefault();
    });
    target.addEventListener('click', function(e) {
      e.preventDefault();
    })
    function checkPrevent(clickEvent) {
      var e = document.createEvent('CustomEvent');
      e.initCustomEvent('foo', true, true, null);
      target.dispatchEvent(e)
      console.log('foo event: ' + e.defaultPrevented);
      clickEvent.preventDefault();
      console.log('click event: ' + clickEvent.defaultPrevented)
      var e2 = document.createEvent('MouseEvent');
      e2.initMouseEvent('click', true, true, null, null, 0, 0, 0, 0, false, false, false, false, 0, null)
      target.dispatchEvent(e2);
      console.log('fake click event: ' + e2.defaultPrevented)
    }
    </script>
</body>
</html>
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers