I made a behavior for span elements that simply toggle themselfes
between add/cancel state and additionally toggle some element on the
page (i.e. a form) on onclick event - http://gist.github.com/118542.
It works fine, but the problem is when this is used to show i.e. a
form. In this case I'd like onclick event handler to be executed when
the form is submitted, so that I can hide the form and update span
element. The main problem is that I can't simply fire onclick event on
span element, which would solve the problem.
On 27 May, 11:33, szimek <szi...@gmail.com> wrote:
> Hi,
> I made a behavior for span elements that simply toggle themselfes
> between add/cancel state and additionally toggle some element on the
> page (i.e. a form) on onclick event -http://gist.github.com/118542.
> It works fine, but the problem is when this is used to show i.e. a
> form. In this case I'd like onclick event handler to be executed when
> the form is submitted, so that I can hide the form and update span
> element. The main problem is that I can't simply fire onclick event on
> span element, which would solve the problem.
> Do you have any other ideas how to solve it?
Assuming the form and the span tags are closely related, why not just
create the behavior on a containing element?
Then you can have an internal function in the behavior, that can be
called from both onclick and onsubmit events.
If you use event delegation with Low Pro, it's actually quite simple.
I recently gave a very brief intro to Low Pro at a local JavaScript
meetup, and for that purpose I created a small behavior for a form,
that has both an onsubmit event listener as well as an event delegate.
You can check out the details http://roderick.dk/blog/2009/05/07/introduction-to-low-pro-for-protot... and from studying the example a bit, I am pretty sure it becomes clear
how to create behaviors that use containing elements.
I agree with Morgan's take which is to use Event.delegate within a
container element to easily have access to both the span and the form
from a single behavior.
Otherwise, you can indeed execute a behavior outside the behavior. i.e
var example = new Link.Toggle($('some-element'));
then
example.onclick(event);
or you can access the instances of a behavior and find the right one
that way:
Thanks for both tips and for the link to the presentation - I totally
forgot about Event.delegate and was using event.element().match
(selector) inside if/else statements - it's actually the same, but
Event.delegate is cleaner.
On 31 Maj, 09:50, Nathan <XGamer...@gmail.com> wrote:
> I agree with Morgan's take which is to use Event.delegate within a
> container element to easily have access to both the span and the form
> from a single behavior.
> Otherwise, you can indeed execute a behavior outside the behavior. i.e
> var example = new Link.Toggle($('some-element'));
> then
> example.onclick(event);
> or you can access the instances of a behavior and find the right one
> that way: