Skip to content Skip to sidebar Skip to footer

Manually Creating A Javascript Event Object

Say I have the following code: $('#someid').click(function(event) { myFunction(event); }); function myFunction(event) { // do something with event } And I would like to test m

Solution 1:

Well, assuming that's JQuery at the top, the event object shouldn't ever be null.

The event object is a completely different critter between IE and everybody else so unless JQ 100% normalizes the object (I think it builds its own event object from the looks of it), it may still vary in properties that JQuery doesn't use between browsers. To manufacture your own event object I guess I would just use a for x in loop to see what's inside and build an object hash that simulates that.

So on this page something like:

$('#adzerk1').click( function(event){
    console.log('fakeEvent = {');
    for(x in event){ console.log( x + ':' + event[x] + ',\n')}
    console.log('}');
} );

$('#adzerk1').click();

will return all properties and their values for that event in the console box of firebug. You'd have to make it recursive and test to get all the innards of properties of event that are objects themselves, however. Otherwise I've set it up so that all you would have to do is cut and paste the results into your JS and then delete the last comma before the closing curly bracket.

Post a Comment for "Manually Creating A Javascript Event Object"