zq29 Posted October 31, 2006 Share Posted October 31, 2006 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. Quote Link to comment Share on other sites More sharing options...
zq29 Posted October 31, 2006 Author Share Posted October 31, 2006 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 Link to comment Share on other sites More sharing options...
tomfmason Posted October 31, 2006 Share Posted October 31, 2006 I was going to say .click for that but you got it.I don't know what to call it ether. I know the the onclick is an event so.. Maybe you can call it the event referrence or function ..?? Glad you got it.Tom Quote Link to comment Share on other sites More sharing options...
zq29 Posted November 1, 2006 Author Share Posted November 1, 2006 [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?! Quote Link to comment Share on other sites More sharing options...
tomfmason Posted November 1, 2006 Share Posted November 1, 2006 It will work in IE if you use getElementById . That will also work in FF and Opera.[code]document.getElementById('yourId').click = something(); //or .onclick[/code]Good Luck,Tom Quote Link to comment Share on other sites More sharing options...
zq29 Posted November 1, 2006 Author Share Posted November 1, 2006 Thanks Tom, I actually just ended up re-writing the function this was used in to avoid the issue, although, I will definitly keep this in mind should I need to do this again.Thanks again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.