Calling a function from onclick event

Hi,

When I run the following code I get error: "Microsoft JScript runtime error: object expected".  Does anybody know why this happens and how to work around the problem?

Thank you for your help.

Alexandra

<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="GENERATOR" Content="Microsoft Visual Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8">

<script language="javascript">

      function secondCall()      {
        alert("hi");
      }

      
      function firstCall()      {
       document.write("<input type = 'button' value = '2nd click' onclick = 'secondCall()'>");
      }

</script>

</HEAD>


<BODY>
      <input type="button" value="click me" name="myButton" onclick = "firstCall()">
</BODY>
</HTML>
ekstern05Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZvonkoSystems architectCommented:
Check this:

<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="GENERATOR" Content="Microsoft Visual Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8">

<script language="javascript">

     function secondCall()     {
       alert("hi");
     }

     
     function firstCall()     {
      document.getElementById("dBtn").innerHTML="<input type = 'button' value = '2nd click' onclick = 'secondCall()'>";
     }

</script>

</HEAD>


<BODY>
<form>
<div id="dBtn">
     <input type="button" value="click me" name="myButton" onclick = "firstCall()">
</div>
</form>
</BODY>
</HTML>

The document.write() does overwrite the complete page structure and the script secondCall() is gone.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZvonkoSystems architectCommented:
And perhaps you are looking for this:

<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="GENERATOR" Content="Microsoft Visual Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8">

<script language="javascript">

     function secondCall()     {
       alert("hi");
     }

     
     function firstCall(theBtn)     {
       theBtn.value = '2nd click';
       theBtn.onclick = function(){secondCall()};
     }

</script>

</HEAD>


<BODY>
<form>
     <input type="button" value="click me" name="myButton" onclick = "firstCall(this)">
</form>
</BODY>
</HTML>

0
ekstern05Author Commented:
Thanks Zvonko, that's exactly what I was looking for!

Alexandra
0
ZvonkoSystems architectCommented:
You are welcome.
0
Tony_GoodmanCommented:
It looks like the document.write is overwriting the current file, here is a workaround that has the same results.
Hope this helps,
-Tony

<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="GENERATOR" Content="Microsoft Visual Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8">
<SCRIPT language="javascript">
     function Call(){
          if(document.TG.myButton.value=="Click Me"){
               document.TG.myButton.value="2nd Click";}
          else{
               alert("hi");}
}

</SCRIPT>
</HEAD>

<FORM Name="TG">
<BODY>
     <input type="button" value="Click Me" name="myButton" onclick="Call()">
</BODY>
</FORM>
</HTML>
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.