onclick -- dynamic definition

Discussion in 'JavaScript' started by gerases, Apr 27, 2006.

  1. #1
    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 + ")");
     
    gerases, Apr 27, 2006 IP
  2. Danny

    Danny Active Member

    Messages:
    732
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    78
    #2
    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
     
    Danny, Apr 27, 2006 IP
  3. gerases

    gerases Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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!
     
    gerases, Apr 27, 2006 IP
  4. Danny

    Danny Active Member

    Messages:
    732
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    78
    #4
    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
     
    Danny, Apr 27, 2006 IP
  5. gerases

    gerases Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    gerases, Apr 27, 2006 IP
  6. Danny

    Danny Active Member

    Messages:
    732
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    78
    #6
    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
     
    Danny, Apr 27, 2006 IP
  7. gerases

    gerases Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Danny,

    Thanks for your help. I really appreciate it.

    Cheers,
    Sergei
     
    gerases, Apr 28, 2006 IP