Get up to 80 % extra points for free! More info:

Discussion: Lesson 1 - Introduction to jQuery

Back

 

Comments
Avatar
oxana
Member
Avatar
oxana:6/23/2019 20:28

Please, explain the last paragraph (with some examples):
"The syntax above is useful when we don't know to what event we'll react at the time when writing the code. We can pass the event name as string later."

 
Reply
6/23/2019 20:28
Avatar
Replies to oxana
David Capka Hartinger:6/24/2019 5:17

Hi Oxana,
thank you for your interest :) Let's consider you have an application where the user can choose which event triggers an action. In the code, we don't know which event the user chose since it's determined when the program is running. So we'll store the event name in a variable and use the bind() method to respond to it:

let eventName = "click"; // This value comes from the user somehow
// it could also be let eventName = "mouseenter";
$(#element_id).bind(eventName, function() {...}); // We cannot use just .click() since there can be any event in the variable

If we knew it's always the click event we want to react to, we wouldn't need bind():

$(#element_id).click(function() {...});

Is it more clear now?

Edited 6/24/2019 10:18
Reply
6/24/2019 5:17
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
oxana
Member
Avatar
Replies to David Capka Hartinger
oxana:6/24/2019 10:11

Yep! I got it. Thanks a lot

 
Reply
6/24/2019 10:11
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

3 messages from 3 displayed.