onclick on JS

Discussion in 'JavaScript' started by gil.fitussi, May 16, 2012.

  1. #1
    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..
     
    gil.fitussi, May 16, 2012 IP
  2. e-abi

    e-abi Member

    Messages:
    122
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    38
    #2
    define function bye(); before defining fuction hi();
     
    e-abi, May 17, 2012 IP
  3. gil.fitussi

    gil.fitussi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    nop..

    still the same problem..

    bye is not defined

    [TABLE]
    [TR]
    [TD][​IMG][/TD]
    [TD]
    bye();[/TD]
    [/TR]
    [/TABLE]
     
    gil.fitussi, May 17, 2012 IP
  4. gil.fitussi

    gil.fitussi Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    bye is not defined
    onclick()onclickevent = click clientX=38, clientY=18





    [TABLE]
    [TR]
    [TD][​IMG][/TD]
    [TD]
    bye();[/TD]
    [/TR]
    [/TABLE]
     
    gil.fitussi, May 17, 2012 IP
  5. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Don't use document.write() in the <head> section of your documents. It will only cause problems. It's generally best to use DOM methods to create content on a page, but for this simple case, you could use:
    
    <!DOCTYPE html>
    <html>
    <head>
    <script language="javascript" type="text/javascript">
    
    function hi() {
    alert("hi");
    document.getElementById('myButton').onclick=bye;
    }
    
    function bye() {
    alert("bye");
    document.getElementById('myButton').onclick=hi;
    }
    
    </script>
    </head>
    <body>
    <script language="javascript" type="text/javascript">
    document.write("<input id='myButton' type='button' value='push' onclick='hi()'/>");
    </script>
    </body>
    </html>
    
    Code (markup):
     
    rainborick, May 17, 2012 IP