Add event to object in javascript

// Find all the ‘li’ elements, to attach the event handlers to them

var li = document.getElementsByTagName(“li”);
for ( var i = 0; i < li.length; i++ ) {
// Attach a mouseover event handler to the ‘li’ element,
// which changes the ‘li’s background to blue.
li[i].onmouseover = function() {
this.style.backgroundColor = ‘blue’;
};
// Attach a mouseout event handler to the ‘li’ element
// which changes the ‘li’ background back to its default white
li[i].onmouseout = function() {
this.style.backgroundColor = ‘white’;
};
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s