jQuery - Click event before inline one

How to add a click event that has to be executed BEFORE the inline one.

by Luca De Nardi

HTML

<a href="#" onclick="alert('Inline event (1)')">First Link</a>
<br>
<a href="#" onclick="alert('Inline event (2)')">Second Link</a>
<br>
<a href="#" onclick="alert('Inline event (3)')">Third Link</a>

JavaScript

$(document).ready(function(){
    //For each element of the type
    $('a').each(function(a){
        //Function that will be fired BEFORE the inline one
        //Remember the ';' at the end!
        var newFunction = "alert('Script Event');";
        //Compose the click function (as string)
        var newClick = newFunction + ' ' + $(this).attr("onclick");
        //Transform it into a real function
        newClick = new Function(newClick);
        //Remove the default click event and append the new ones
        $(this).removeAttr('onclick').click(newClick);
    })
})