presented at
November 12, 2012
via Introduction to Events by Quirksmode.org
<a href="#" onClick="alert('hello world')">Show Pointless Alert</a>
via JavaScript Events Best Practices on Stackoverflow
OMGWTFBBQ?!?!
element.addEventListener('click', functionName, false);
click not onclick
via Advanced Event Registration Models by Quirksmode
element.attachEvent('onclick', functionName);
via JavaScript vs. jQuery by AlbertoPL
(Pseudo code)
$('selector').event(function() {
//Your Code Goes Here
});
$('a').click(function() {
alert('Hello World');
});
Just kidding. There's more…
this is a special variable that refers to the specific element triggering the event.
$('a').click(function () {
alert(this.href);
});
You can use this as a selector and use jQuery methods just like any other element.
$('li').click(function() {
alert( $(this).text() );
});
Using keyup() attached to an input field, we can check if the value of the input meets a certain criteria.
$('input').keyup(function() {
$(this).attr('class', '');
var val = $(this).val();
if( val.length < 4 ) {
$(this).addClass( 'bad' );
}
});
This is useful for checking if certain fields are completed or you want to perform an AJAX request.
$('form').submit(function() {
if( condition == false ) {
//returning false cancels the form from being submitted.
return false;
}
//returning true will allow the form to be submitted.
return true;
});
Displays a confirmation box asking if you want to search for the entered term on Google.
$('form').submit(function() {
return confirm('Are you sure you want to search Google?');
});
jQuery passes an event object to the function being triggered by the event.
The event object is normalized across different browsers and contains extra data about the context of the event.
Get x most recent followers and move them around the screen as a swarm based on where you click.
$(document).mouseup(function(e) {
imgAttract(e.pageX, e.pageY);
});
via Follow the Twitter by Me
Tweet me, @kingkool68, or contact me.