[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

jQuery how do  you attach a function to a click event, not a delegate?

Posted on 2009-05-14
8
Medium Priority
?
360 Views
Last Modified: 2012-05-07
I want to attach a function to a click event, but I don't want to write an inline function since I might want to call the same fuction from two places.

I know how to do this:
$(document).ready(function() {
    $('#example_button').click(function() {
        window.alert("You clicked me!");
    });
});


How do I do this:
$(document).ready(function() {
    $('#example_button').click(standardFunction());
}

function standardFunction(){
        window.alert("You clicked me!");
}
0
Comment
Question by:jackjohnson44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 14

Assisted Solution

by:ziffgone
ziffgone earned 800 total points
ID: 24387966
Easy:


$(document).ready(function() {
    $('#example_button').click(function() {
        standardFunction();
    });
});
function standardFunction(){
        window.alert("You clicked me!");
}

Open in new window

0
 
LVL 12

Expert Comment

by:alien109
ID: 24388062
Another thing you can do is call the function within the scope of the button

$(document).ready(function() {
    $('#example_button').click(function() {
        standardFunction.call(this);
    });
});
function standardFunction(){
        window.alert("You clicked me!");
}
0
 

Author Comment

by:jackjohnson44
ID: 24388321
Thanks, but can one of you tell me the difference in the .call?

Does that do anything extra for me?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Expert Comment

by:ziffgone
ID: 24388409
Not sure.

@alien109: What would the benefit be of using ".call"?

Regards...
0
 
LVL 12

Expert Comment

by:alien109
ID: 24388513
using call, allows you to call a function within the scope of an object.

For example:

function test() {
    alert(this.color);
}

a = {color:"red"};
b = {color:"blue"};

test.call(a) // would result in an alert with the text "red"
test.call(b) // would result in an alert with the text "blue"

So in your standardFunction, you say, "you clicked me!". However you don't actually know who "me" is. So, by using call, the function can get executed in the scope of the button object that was clicked. Using the keyword "this" within that function would reference the button object. In other words, the "me" in this case would actually be the button object: $('#example_button')
0
 
LVL 12

Accepted Solution

by:
alien109 earned 1200 total points
ID: 24388590
Sometimes I don't explain stuff well... So I apologize if I'm not making sense.

Essentially the reason you MAY want to do this (I'm not saying you do!) is so that if you want to actually do something to the button that was clicked, you'd need to know which button you clicked. By using call, you can simply refer to the button that was clicked, by using "this" within the function.
0
 

Author Closing Comment

by:jackjohnson44
ID: 31581617
Thanks Guys, good explanation alien!
0
 
LVL 14

Expert Comment

by:ziffgone
ID: 24389484
That's cool alien109, thanks...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question