Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: onclick on JS

  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onclick on JS

    hi everybody..


    i have this simple code:

    <html>
    <head>
    <script language="javascript" type="text/javascript">
    document.write("<input type='button' value='push' onclick='hi()'/>");
    function hi() {
    alert("hi");
    document.write("<input type='button' value='enter' onclick='javascript: bye()'/>");




    }
    function bye() {
    alert("bye");


    }
    </script>
    </head>


    <body>




    </body>
    </html>

    if i try to run it on chrom its works well..

    but on IE or FireFox its not define the bye() function..

    whats the reason?! and how i can solve it?..

    thanks very much..

  • #2
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if i define
    function bye(); before defining fuction hi();
    its not working eather..

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    <html>
    <head>
    <script language="javascript" type="text/javascript">

    function hi() {
    alert("hi");
    }
    function bye() {
    alert("bye");
    }
    </script>
    </head>

    <body>
    <script language="javascript" type="text/javascript">
    document.write("<input type='button' value='push' onclick='hi()'/>");
    document.write("<input type='button' value='enter' onclick='javascript: bye()'/>");
    </script>

    </body>
    </html>

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    document.write shouldn't be used for creating html elements or anything so simple - it has its place in advanced javascript, but can and should be avoided for simple operations like this. Much simpler is to hardcode the button and change its attributes when the function is called:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function hi(btn) {
    alert("hi");
    btn.value="enter";
    btn.onclick=bye;
    }
    function bye() {
    alert("bye");
    
    }
    </script>
    </head>
    
    <body>
    <input type='button' value='push' onclick='hi(this)'/>
    
    </body>
    </html>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •