Reputation: 15
I have a problem with an excercise, you can see the code below. I dont understand then event object evt, and also the book is not clear about that. Example in the code why it use showMessage(evt) and alert(evt.data.message), it is necessary?? THanks for help Frank
var linkVar = {message:'Hello from a link'};
var pVar = {message: 'Hello from a p'};
function showMessage(evt) {
alert(evt.data.message);
}
$('a')on('mouseover' , linkVar, showMessage);
$('p')on('mouseover' , pVar, showMessage);
Upvotes: 0
Views: 126
Reputation: 1962
It will help you have running code as an example:
// Variable definitions
var linkVar = {message:'Hello from a link'};
var pVar = {message: 'Hello from a p'};
// What to do on mouseover event on which elements and what to call when that happens
$('a').on('mouseover' , linkVar, showMessage);
$('p').on('mouseover' , pVar, showMessage);
// Function definition (it will be called everytime cursor mouseovers over elements A and P)
function showMessage(evt) {
alert(evt.data.message);
}
From here, you have the .on method which will say:
data Type: Anything Data to be passed to the handler in event.data when an event is triggered.
so anything will be passed as event.data so to get to the field you need you will use evt.data.message
Hope this clears your confusion.
Here it is in a fiddle:
https://jsfiddle.net/vdhobqny/
Upvotes: 1