0

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 myFunction() with an eval statement, without doing something like using eval('$("#someid").click()');

Is there a way to manually create an event object in javascript that would work with an eval statement?

Also if myFunction is only used as an event handler, can it always assume event is not null?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Interesting. So basically, using .click() in my test code is probably the best approach to set the proper event object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.