Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Change A function

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
Hey all, Here is an example to somthing that I want to do, lets first look at some code
Code:
<script>
function = funOne()
{
   alert('This is #1');
}
function = funTwo()
{
   alert('this is #2');
}
</script>
<body>
   <button onclick="funOne()">
</body>
What I want is when I press the button, an alert is kicked off and then I want the button to change from
Code:
onclick="funOne()"
to
Code:
onclick="finTwo()"
I want to change the onclick property for the button, can this be done? What element do I have to change?

Thanks,
-T

-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
This is what you would need to do. Pass 'this' (button obj) on the onclick of the button. Then change the onclick property of the button element to call the other function.

Code:
<script type="text/javascript">
function funOne(obj)
{
   alert('This is #1');
   obj.onclick = funTwo;
}
function funTwo()
{
   alert('this is #2');
}
</script>
<body>
   <button onclick="funOne(this)">Hello</button>
</body>


[monkey][snake] <.
 
OK, now that I am still learning O-Master, What is wrong here?
Code:
<html>
	<head>
		<script type="text/javascript">
			function funOne(obj)
			{
	   			document.getElementById(obj).style.display ='inline';
				document.getElementById(obj).onclick = funTwo(obj);
	   			//obj.onclick = funTwo;
			}
			function funTwo(obj)
			{
	   			document.getElementById(obj).style.display ='none';
			}
		</script>
	</head>
	<body>
		<button onclick="funOne('noshow')">Change Me</button>
		<p id="noshow" style="display:none">
			I am showing you this right now
		</p>
	</body>
</html>
So When I press the button nothing happens, it looks like it shows then dosnt show. What am I doing wrong. Thanks again

-How important does a person have to be before they are considered assassinated instead of just murdered?
-Need more cow bell!!!

 
Ok if you want to get this to work like that, here's what you need to do. Since you are changing the onclick event on the BUTTON, you need to still pass a reference of "this" to the function funOne. You also passed the id of the <p>, which is fine.

After you enter funOne, you need to make sure you account for "this" getting pulled in, so you add another argument
in your function : function funOne([!]obj[/!], pObj)

Then you change the onclick of the button, which I called obj. Since you added a parameter to funTwo, you have to write it a different way. I pass "noshow" to the function. You CAN'T pass pID to the function, because the value of pID will be lost after the funOne function ends.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] xml:lang="en">
    <head>
        <script type="text/javascript">
            function funOne(obj, pId)
            {
                document.getElementById(pId).style.display ='inline';
                obj.onclick = function() {funTwo("noshow")};
                   //obj.onclick = funTwo;
            }
            function funTwo(obj)
            {
                   document.getElementById(obj).style.display ='none';
            }
        </script>
    </head>
    <body>
        <button onclick="funOne(this, 'noshow')">Change Me</button>
        <p id="noshow" style="display:none">
            I am showing you this right now
        </p>
    </body>
</html>

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top