Skip to content

Instantly share code, notes, and snippets.

@carldanley
Created August 28, 2012 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carldanley/3504350 to your computer and use it in GitHub Desktop.
Save carldanley/3504350 to your computer and use it in GitHub Desktop.
Box Example
function createBoxes(){
var html = '';
var boxesToCreate = 50;
//create the container node
var container = document.createElement( 'div' );
container.className = 'container';
//create the boxes and add them to the container
for( var i = 0; i < boxesToCreate; i++ ){
html += '<div data-box-number="' + i + '" class="box">Box ' + i + '</div>';
}
container.innerHTML = html;
//add the container with the boxes to the document body
document.body.appendChild( container );
return container;
};
function bindBoxEvents( containerElement ){
//bind a click function to the boxes found within the containerElement
//we used jQuery here to simplify the example
$( containerElement ).find( '.box' ).on( 'click', showBoxNumber );
};
function showBoxNumber( e ){
//cleanup the event and find the target
e = e || window.event;
var target = e.srcElement || e.target;
//get the box number now
var boxNumber = target.getAttribute( 'data-box-number' );
alert( 'You clicked Box #' + boxNumber );
};
var container = createBoxes();
bindBoxEvents( container );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment