DOM Enlightenment Book

by landongw

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>
<div>no need to click, we programmatically trigger it</div>





























<h4 style="border-top:1px solid #ADADAD;padding-top:20px;margin-top:20px;"><strong>&larr; Code Example from <a href="http://www.domenlightenment.com/" target="_blank">DOM Enlightenment Book</a> by <a href="http://www.codylindley.com" target="_blank">Cody Lindley</a></strong></h4>

<p style="background-color:#fff">JavaScript code is executed on page load. Click the "Run" button in the top bar to re-run the JavaScript code. All <code>console.log()'s</code> are logged to the <a href="http://getfirebug.com/firebuglite" target="_blank">Firebug Lite</a> console below &darr;.</p>

JavaScript

var divElement = document.querySelector('div');

//setup click event that will be simulated
/* divElement.addEventListener('click',function(event){
    console.log(Object.keys(event));
},false); */

//create simulated mouse event 'click'
var simulateDivClick = document.createEvent('MouseEvents');

/* setup simulated mouse 'click' */
initMouseEvent(type,bubbles,cancelable,view,detail,screenx,screeny,clientx,clienty,ctrlKey,altKey,shiftKey,metaKey,button,relatedTarget) 

/* simulateDivClick.initMouseEvent('click',true,true,document.defaultView,0,0,0,0,0,false,false,false,0,null,null);
 */
//invoke simulated clicked event
divElement.dispatchEvent(simulateDivClick);