It seems extremely unlikely that the code you listed is actually doing what
you think it's doing.

In your updated example (the "to this" part), you're calling do_something()
*before* the click event happens. Don't you want to call it *when* the click
event happens?

You have this line of code:

    $("#my_div").click( do_something() );

Here's what that does:

1. It calls the do_something function immediately when the document ready
callback is called - long before the click event happens.

2. It takes the *return value* of the do_something function and establishes
that as the handler for the click event. In other words, it assumes that the
do_something function returns *another* function, and that function is the
click handler.

3. Later, when #my_div is clicked, it calls that other function.

Is that actually what you want? You haven't shown us what the do_something
function looks like. A function that returns another function can be a very
powerful technique in JavaScript. But I'm just about certain that this isn't
what you're doing here. Or am I wrong about that?

Could you please post a link to a test page instead of the code snippets?
I'm just taking guesses as to what your code really looks like, since so
much of it is missing.

What the heck, here's one more guess. Back to your original code:

> 5  $(document).ready(function() {
> 6         $("#my_div").click(function () {
> 7                do_something
> 8          });
> 9  });

Is it possible that this is actually literally what your code looks like
(minus the line numbers)? I assumed that "do_something" was just a
placeholder for some other actual code. But is that what the code really
looks like? If do_something is the name of a function, just putting the name
by itself won't do anything. You'd have to use do_something() to actually
call the function. Maybe that's all the problem was, the missing ()?

Well, enough guessing games. Post a link to your actual code. Please! :-)

-Mike

> From: nachocab
> 
> Hi everyone,
> I found the mistake, although I'm not quite sure why it 
> works, but it does.
> Change this:
> __app.js___________________________________
> 5  $(document).ready(function() {
> 6         $("#my_div").click(function () {
> 7                do_something
> 8          });
> 9  });
> To this:
> 5  $(document).ready(function() {
> 6         $("#my_div").click( do_something() );
> 7         function do_something (){...};
> 8  });
> 
> Thanks for the inspiration!
> 
> Nacho
> On Dec 14, 7:25 pm, "Michael Geary" <m...@mg.to> wrote:
> > It's pretty hard to tell what might be wrong from the code snippet. 
> > Can you post a link to a test page instead?
> >
> > -Mike
> >
> > > From: nachocab
> >
> > > You're right:
> > > __test.js__________________________
> > > 1  test("bind div again", function() {
> > > 2         $("#my_div").click();
> > > 3         ok( check_if_it_worked, "working") //Here it fails!
> > > 4  });
> > > __app.js___________________________________
> > > 5  $(document).ready(function() {
> > > 6         $("#my_div").click(function () {
> > > 7                do_something
> > > 8          });
> > > 9  });
> >
> > >  The test fails because line 2 never calls the function 
> in line 7. 
> > > Any ideas? I'm clueless... Thanks!
> >
> > > On Dec 14, 2:59 pm, "Derrick Jackson" 
> <derrick.jackso...@gmail.com>
> > > wrote:
> > > > Nacho,
> >
> > > > I am new to jQuery and may not have a lot to offer, but 
> does the 
> > > > second function actually fail or does it not run?
> >
> > > > On 12/14/08, nachocab <nacho...@gmail.com> wrote:
> >
> > > > > Hi,
> > > > > I have a test that passes if I run it by itself, but if I
> > > duplicate
> > > > > the code and run both of them, the second one fails. What
> > > am I doing
> > > > > wrong?
> >
> > > > > __test.js___________________________________
> > > > > test("bind div", function() {
> > > > > $("#my_div").click();
> > > > > ok( check_if_it_worked, "working") });
> >
> > > > > test("bind div again", function() { $("#my_div").click(); ok( 
> > > > > check_if_it_worked, "working") //Here it fails!
> > > > > });
> >
> > > > > __app.js___________________________________
> > > > > $(document).ready(function() {
> > > > >   $("#my_div").click(function () {
> > > > >     do_something
> > > > >   });
> > > > > });
> >
> > > > > Thanks!
> >
> > > > > Nacho
> >
> > > > --
> > > > Sent from Gmail for mobile | mobile.google.com
> 

Reply via email to