Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Calling a function from onclick event

Posted on 2004-10-29
5
Medium Priority
?
212 Views
Last Modified: 2008-02-26
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>
0
Comment
Question by:ekstern05
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 1000 total points
ID: 12447779
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 12447795
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
 

Author Comment

by:ekstern05
ID: 12447944
Thanks Zvonko, that's exactly what I was looking for!

Alexandra
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12447950
You are welcome.
0
 
LVL 1

Expert Comment

by:Tony_Goodman
ID: 12447982
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question