Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > JavaScript > Re: Appending E...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 10 Topic 35480 of 37133
Post > Topic >>

Re: Appending Events

by RobG <rgqld@[EMAIL PROTECTED] > May 6, 2008 at 08:13 PM

On May 6, 9:06 pm, dunerunner <tpdi...@[EMAIL PROTECTED]
> wrote:
> On May 5, 7:13 pm, apatheticagnostic <apatheticagnos...@[EMAIL PROTECTED]
>
> wrote:
>
>
>
> > > What I have to begin with is button A only.
> > > User clicks button A.
> > > In the onclick event for button A I have a function that creates
> > > button B.  The function that creates button B attaches an onclick
> > > event to button B.  After button B is created (while still in the
> > > onclick event handler) I need to add another function to button B's
> > > onclick event.
>
> > > Sorry if this isn't clear, but it's a strange requirement.
>
> > > Thanks for trying to help.
>
> > If something like
>
> > buttonb.onclick = (function(original) {
> >                      return function() {
> >                        original();
> >                        new_function();
> >                      };
> >                     })(buttonb.onclick);
>
> > doesn't work, would using the level 2 DOM handlers work for you? i.e.
> > buttonb.addEventListener('click', new_func);
>
> That sounds like an interesting alternative.  However, I'm finding
> that I can't even access button B while I'm still inside button A's
> onclick event handler because it was only just created by the previous
> function and not yet accessible, so I think it is not possible this
> way.  I need to somehow refresh the page before accessing button B.
>
> Thanks for all of your great ideas, I appreciate them.

Presumably you kickoff the function that creates the button and adds
the onclick handler.  That function should return a reference to the
button so you can add your onclick handler, but since you are using
getElementById and say you can't get a reference to it I'll presume
that doesn't happen.

The best solution is to have the function return a reference to the
button that it creates so that you can do stuff wtih it.  But you say
you can't change that function so... a (rather horrible) alternative
is to use setTimeout with a short delay to add the onclick handler,
e.g.

  function foo() {
    ...
    addButtonB();
    setTimeout( function(){
       var buttonB = do***ent.getElementById('buttonB');

       /* add hanlder to buttonB using one of the
       ** methods suggested in this thread
       */

    }, 10);
  }

you may want to play with the length of the timeout, 10ms should be
OK.


--
Rob
 




 10 Posts in Topic:
Appending Events
dunerunner <tpdietz@[E  2008-05-05 06:38:09 
Re: Appending Events
Ugo <privacy@[EMAIL PR  2008-05-05 16:24:49 
Re: Appending Events
dunerunner <tpdietz@[E  2008-05-05 08:34:54 
Re: Appending Events
Ugo <privacy@[EMAIL PR  2008-05-05 17:52:22 
Re: Appending Events
dunerunner <tpdietz@[E  2008-05-05 12:11:52 
Re: Appending Events
Ugo <privacy@[EMAIL PR  2008-05-06 00:09:24 
Re: Appending Events
apatheticagnostic <apa  2008-05-05 16:13:18 
Re: Appending Events
dunerunner <tpdietz@[E  2008-05-06 04:06:44 
Re: Appending Events
apatheticagnostic <apa  2008-05-06 15:29:52 
Re: Appending Events
RobG <rgqld@[EMAIL PRO  2008-05-06 20:13:48 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Oct 11 5:56:13 CDT 2008.