HappyDude
08-05-2002, 04:47 AM
I have a function that is supposed to take a button and change its onclick so that when pressed, it changes the value of an object to something. The problem is, the object I need the onclick to change is one of the function arguments, and thus, when the button is pressed, the argument no longer exists because by then, all the values in the function will be gone, because the function will have ended... what I was thinking of doing is rather than putting the name of the object in onclick, putting the object itself there... unfortunately, all my attempts to do so have failed horribly, and I'm not sure it's possible...
Here's a little example I made to try to help explain my problem...
<html>
<head>
</head>
<body>
<input id='AButton' type='button' value='A Button'>
<input id='AnotherButton' type='button' value="Another Button"><br>
<input type='button' value="Tell me the value of blah!" onclick='alert(blah.myvalue)'>
<input type='button' value="Tell me the value of blah2!" onclick='alert(blah2.myvalue)'>
<script>
//These are the objects whose values should be changing
var blah=new Object()
blah.myvalue="I have a value. I'm special."
var blah2=new Object()
blah2.myvalue="I have a value too. YAY!"
//This function is supposed to take a button, an object and a value,
//then change the onclick of the button to make the object's 'myvalue' property become the value parameter...
// but doesn't work, read comments
function MakeButtonChangeBlahValue(thebutton,theblah,thevalue)
{
//Doesn't work because by the time the button is pressed, theblah doesn't exist because the function already finished
thebutton.onclick='theblah.myvalue='+thevalue
//Doesn't work. Why? Because Javascript doesn't want it to.
thebutton.onclick=theblah+'.myvalue='+thevalue
//Read above comment
thebutton.onclick=theblah.myvalue+'='+thevalue
//This doesn't give any error, but the problem is that it changes the value instantly, rather than when the button is pressed
// thebutton.onclick=theblah.myvalue=thevalue
}
//'Should' change the onclick of the buttons to change the value of the blahs when they're clicked on... but doesn't because function doesn't work
MakeButtonChangeBlahValue(document.getElementById('AButton'),blah,"This is what I want my value to be")
MakeButtonChangeBlahValue(document.getElementById('AnotherButton'),blah2,"This is what I want my other value to be")
</script>
</body>
</html>
Any ideas anyone?
Here's a little example I made to try to help explain my problem...
<html>
<head>
</head>
<body>
<input id='AButton' type='button' value='A Button'>
<input id='AnotherButton' type='button' value="Another Button"><br>
<input type='button' value="Tell me the value of blah!" onclick='alert(blah.myvalue)'>
<input type='button' value="Tell me the value of blah2!" onclick='alert(blah2.myvalue)'>
<script>
//These are the objects whose values should be changing
var blah=new Object()
blah.myvalue="I have a value. I'm special."
var blah2=new Object()
blah2.myvalue="I have a value too. YAY!"
//This function is supposed to take a button, an object and a value,
//then change the onclick of the button to make the object's 'myvalue' property become the value parameter...
// but doesn't work, read comments
function MakeButtonChangeBlahValue(thebutton,theblah,thevalue)
{
//Doesn't work because by the time the button is pressed, theblah doesn't exist because the function already finished
thebutton.onclick='theblah.myvalue='+thevalue
//Doesn't work. Why? Because Javascript doesn't want it to.
thebutton.onclick=theblah+'.myvalue='+thevalue
//Read above comment
thebutton.onclick=theblah.myvalue+'='+thevalue
//This doesn't give any error, but the problem is that it changes the value instantly, rather than when the button is pressed
// thebutton.onclick=theblah.myvalue=thevalue
}
//'Should' change the onclick of the buttons to change the value of the blahs when they're clicked on... but doesn't because function doesn't work
MakeButtonChangeBlahValue(document.getElementById('AButton'),blah,"This is what I want my value to be")
MakeButtonChangeBlahValue(document.getElementById('AnotherButton'),blah2,"This is what I want my other value to be")
</script>
</body>
</html>
Any ideas anyone?