Thanks for the replies Richard and Michael, that makes sense and now
works!

Just one more question - what if I need to provide arguments to the
function? Don't these need to go in parens which will then cause the
function to be called immediately?

cheers,

DAZ




On Dec 7, 10:55 pm, "Michael Geary" <[EMAIL PROTECTED]> wrote:
> > From: DAZ
>
> > Thanks Michael, the example you gave works great. I'm having
> > some problems with a droppable though, here is the sample code:
>
> > $("#dropper").droppable({
> >   accept: "#dragger",
> >   drop: function(ev, ui) {$(this).append("<br>Dropped!");} });
>
> > The function that is called on the drop is likely to get very
> > complex, so I'd like it in a separate function.
>
> > However, my solution below doesn't work:
>
> >  $("#dropper").droppable({
> >   accept: "#dragger",
> >   drop: dropped()
> > });
>
> > function dropped() { $(this).append("dropped!") }
>
> That isn't the same as your original code. Here's the problem:
>
>     drop: dropped()
>
> That *calls* the dropped function immediately - not when the drag and drop
> occurs, but when you call the .droppable() method initially.
>
> Instad, you want to pass a *reference* to the dropped function, by omitting
> the ():
>
>     drop: dropped
>
> By just giving the function name with no parens, you obtain a reference to
> the function instead of calling it immediately.
>
> > The dropped function is getting called as if I change it to:
>
> > function dropped() { alert("dropped!") }
>
> > The alert message does show.
>
> Ah, but when does the alert show? If the code you listed is your actual
> code, the alert shows up too soon, doesn't it?
>
> > So I think that there must be a
> > problem with it accessing $(this).
>
> That is something you have to watch out for, but it's not the problem in
> this particular case (pun unintended but unavoidable).
>
> When you change an anonymous function to a named function that you call
> through a reference to the function, 'this' works the same as before. So if
> your anonymous function worked, then changing it to a reference to a named
> function will work as intended - as long as you don't call the function
> prematurely by appending () to the function name.
>
> -Mike

Reply via email to