Jump to content

Dynamic document references (I'm not sure what to call them!?)


zq29

Recommended Posts

I'm writing a function that accepts a form name, a button name (from within the named form) and a replacement function call (encoded with PHPs urlencode()) as arguments.

I want the function to replace the buttons 'onclick' value with the new supplied function call - I'm almost there, just a slight issue. Here's the code:

[code]function assignId(selectForm,selectName,targetButton,replaceWith) {
    var button = document.getElementById(targetButton);
    var select = eval("document."+selectForm+"."+selectName+".options[document."+selectForm+"."+selectName+".selectedIndex].value");
    replaceWith = unescape(replaceWith);

    var replace = replaceWith.replace("*",select);
    eval("document."+selectForm+"."+targetButton+".onclick = "+replace);
}[/code]

The bit that I'm having issues with is the last line where I'm trying to assign the replacement function call to the buttons 'onclick' event. What it's actually doing, is running the function call that I'm trying to assign. I think I just need to know how I can get the selectForm and targetButton variables attached to the document. part.

Here are a few things that I have tried that are not working:

eval("document."+selectForm+"."+targetButton+".onclick = "+replace);

document.selectForm.targetButton.onclick = replace;

document.eval(selectForm).eval(targetButton).onclick = replace;



Not having much luck - Any suggestions?

Many thanks.
Link to comment
Share on other sites

Ok, I've actually got around my problem like so:
[code]document.forms[selectForm].elements[targetButton].onclick = replace;[/code]
Although, is there still a way to use variables in this [i]thingy-ma-jig[/i] (I still don't know what they're called - DOM references? Any help there?!)
Link to comment
Share on other sites

[quote author=SemiApocalyptic link=topic=113362.msg460726#msg460726 date=1162322898]
Ok, I've actually got around my problem like so:
[code]document.forms[selectForm].elements[targetButton].onclick = replace;[/code]
Although, is there still a way to use variables in this [i]thingy-ma-jig[/i] (I still don't know what they're called - DOM references? Any help there?!)
[/quote]
Ok, my solution works fine in FireFox but not at all in IE. Alternate solution anyone? How can I use variables within this DOM/event reference thing?!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.