Solved

Calling a function from onclick event

Posted on 2004-10-29
201 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
Question by:ekstern05
    5 Comments
     
    LVL 63

    Accepted Solution

    by:
    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
    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
    Thanks Zvonko, that's exactly what I was looking for!

    Alexandra
    0
     
    LVL 63

    Expert Comment

    by:Zvonko
    You are welcome.
    0
     
    LVL 1

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Cisco Complete Network Certification Training

    If you’re an IT engineer or technician, it's time you take your career to the next level. This elite training bundle is brimming with all of the information you need to learn to sit for Cisco CNNA, CCNP, and CCENT certification exams.

    Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    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…

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now