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


|