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 2 of 2
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    22
    Thanks
    13
    Thanked 0 Times in 0 Posts

    EASY - cannot document.write(b) value

    Below is a mock snippet of what I need. I think the issue is

    Code:
    document.write(b)
    because if I change that to an alert, it works fine. I thought I was using the proper syntax, but I guess I am not. Can anyone help a noob?


    Code:
    <html>
    <head>
    <script type="text/javascript">
    function d(){
    a=document.getElementById("txt1").value;
    
    if(a > 1){
    b=a/100;
    document.write(b)
    }
    else{
    b=a;
    alert(b)
    }
    }
    
    </script>
    </head>
    <body>
    <form name="form">
    <h2>This is a form</h2>
    <input type="text" id="txt1" name="txt"/> type here <br />
    <button type="submit" id="btn" onclick="d()">Submit</button>
    </body>
    
    </html>

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,628
    Thanks
    0
    Thanked 648 Times in 638 Posts
    document.write can only be used BEFORE the page finishes loading.

    The simplest alternative to that almost useless command to use in real web pages is innerHTML.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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