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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Creating a button with document.write()

    Below is a simplified version of my script. I'm attempting to use document.write() to create a button. When clicked, it should display 'Hello". This works in Chrome, but in IE or Firefox, the button is displayed, but is not functional.

    A similar post suggested to use createElement(), so I have spent some time researching and experimenting with this function. W3Schools.com shows how to create a button using createElement(), but the button does not have an attached function, so I haven't yet resolved my problem but feel I'm very close.

    I apologize for my inexperience and appreciate any assistance.

    Code:
    <html>
    <script = 'JavaScript'>
    /////////////////////////////////////////////////////////////////
    function DisplayHomePage(){
    document.open();
    document.write("<center><h1>Homepage</h1><br><form><input type='button' value='display hello' onclick='DisplayHello()'></form></center>");
    document.close();
    }
    /////////////////////////////////////////////////////////////////
    function DisplayHello(){
    document.open();
    document.write("<center><h1>Hello</h1></center>");
    document.close();
    }
    /////////////////////////////////////////////////////////////////
    </script>
    <body onload='DisplayHomePage();'>
    You must have JavaScript Enabled!
    </body>
    </html>

  • #2
    New to the CF scene
    Join Date
    Oct 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Below is what I have been able to come up with using CreateElement(), but this only works in Chrome.

    Code:
    function DisplayHomePage(){
    
    document.open();
    document.write("<center><h1>Homepage</h1><br></center>");
    
    var btn=document.createElement("BUTTON");
    var t=document.createTextNode("CLICK ME");
    btn.appendChild(t);
    document.body.appendChild(btn);
    
    btn.onclick = function(){
    DisplayHello();
    }; 
    document.close();
    }

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 584 Times in 565 Posts
    you can't really use both document.write() and createElement(), pick one, and pick createElement().
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    tronovan (10-04-2013)


  •  

    Posting Permissions

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