Debt Consolidation - Find jobs - Find jobs - Premium wordpress themes - Expekt bonuses

PDA

View Full Version : onclick -- dynamic definition


gerases
Apr 27th 2006, 7:34 pm
I have an html table with each row having a button that has its onclick defined as follows:

onclick=edit(parm1,parm2,event) // this is generated from php

Then, I have the edit function defined as something like this:

function edit (parm1, parm2, event)

Well, then in "edit" I need to redefine the onclick definition for the button that called "edit" because parm1 and parm2 have changed. So, I find the button in question and say something like:

str = parm1 + "," + parm2 + "," + "event";
button.onclick = new Function("", "edit(" + str + ")");

After these actions, I click on the button and this time "event" is not defined in "edit". How do I redefine the function so that "event" is passed correctly on subsequent calls to "edit"?

I tried this too:
str = parm1 + "," + parm2 + "," + "event";
button.onclick = new Function("event", "edit(" + str + ")");

Danny
Apr 27th 2006, 7:58 pm
I was trying to do something similar at one stage but couldnt find anyway to re-define the onlick as it is essentially static

The way I got around it was with JSON and AJAX

but you could always use just an innerhtml tag.

By this I mean when you click edit or when whatever the edit function does is complete use

document.getElementByID["element"].innerhtml = somecode

Basically with this say the edit was a button you could completely rewrite the entire button.

It would then re-load it.

Hope this makes sense.

Basically I do it with images (without an onclick) at www.silvertails.net in the season draw menu. But the concept is the same

gerases
Apr 27th 2006, 8:16 pm
I didn't think about innerHTML! This will do it for sure.

I must say though that it works both in IE and an older version of Firefox. It works in IE because as you know in IE, "event" is a global variable. With firefox, netscape, etc, the event object is passed to functions as a parameter. Well, at work I have an old version of Firefox and it works if I jus say:

editBtn.onclick = new Function("e", "edit(" + parms + ")");

-- without mentioning "event" anywhere. If I do, it stops working at work as well :)

At home, I have 1.5.1 and it doesn't work whatever I do.

Anyway, thanks for the great idea!

Danny
Apr 27th 2006, 8:49 pm
Just be aware innerhtml is best used with divs so wrap a div around the button then it wont move out of allignment etc.

Some javascript programmers don't like innerhtml but it solves a few problems for me so I use it

gerases
Apr 27th 2006, 9:01 pm
That's a cool tip about div's -- I didn't know that. As for other people not liking innerHTML, I kinda understand that -- it's a hack in a way -- but like you said, it does solve a few problems.

Danny
Apr 27th 2006, 9:13 pm
oh yeah for the DIV give it an ID and name so you can reference it and do the innerhtml on the div y reference

gerases
Apr 28th 2006, 5:48 am
Danny,

Thanks for your help. I really appreciate it.

Cheers,
Sergei