Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between jquery.simulate.js and jQuery event methods?

The jQuery UI source library contains a jquery.simulate.js file for simulating mouse and keyboard events. What is the benefit of using that simulate function instead of using jQuery event methods when testing jQuery UI stuff?

like image 880
Zardoz Avatar asked Oct 13 '22 19:10

Zardoz


1 Answers

The jquery.simulate.js file is used only to simulate events - it doesn't provide a full event management system like jQuery does. This means, it creates a fake event object, and then sends this event to the DOM. This can be compared to jQuery's .trigger() method, which does a similar thing.

However, there are many properties on an event object that can be modified, including whether it should bubble or not, what the source element was, whether any special keys were pressed, the exact x and y locations the mouse was clicked, and so on. jQuery hides this from us when we call .trigger(), by setting only the necessary properties to some default values. jquery.simulate.js on the other hand is designed for testing (notice that the file is located under "jquery-ui/tests/"). For testing purposes, you'd want more fine-grained control over the event object to better mimic real user interaction.

Long story short, unless you're doing testing, most likely you won't need jquery.simulate.js at all. Even if you want some of the functionalities it provides, be warned that the file isn't officially part of the jquery-ui library.

like image 177
David Tang Avatar answered Oct 18 '22 04:10

David Tang