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

    Question how to show a number greater than earlier?

    Hi Everyone.!
    I have to create a quite simple javascript but being a newbie I'm having trouble in it.
    It's a javascript about showing a value greater than the earlier for a variable.
    Let me show you.
    Code:
    var count=2o
    var countNow=count+1
    What should I use here to show 21
    Please help me.

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,755
    Thanks
    41
    Thanked 191 Times in 190 Posts
    alert("countNow");

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by alykins View Post
    alert("countNow");
    Hey thanks for quick reply but I'm willing to show countNow in document.write . how should i do that?

  • #4
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,755
    Thanks
    41
    Thanked 191 Times in 190 Posts
    check out response number 2 in this post where he has document.write highlighted

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #5
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry..!
    Still can't understand.
    Is there any other way to do this?

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,371
    Thanks
    11
    Thanked 591 Times in 572 Posts
    Quote Originally Posted by moinism View Post
    Hey thanks for quick reply but I'm willing to show countNow in document.write . how should i do that?
    huh?

    Code:
    document.write(countNow);
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    Use DOM methods for display:-

    Code:
    <span id = "num">20</span><br><br>
    <input type = "button" value = "Add One" onclick="addOne()">
    
    <script type = "text/javascript">
    var countNow = 20
    function addOne() {
    countNow = countNow+1;
    document.getElementById("num").innerHTML = countNow;
    }
    </script>

    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #8
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    document.write(countNow);

  • #9
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by jmlwebdesign View Post
    document.write(countNow);
    document.write is baddddd.........

    In reference to the OP's code...

    var count=2o
    var countNow=count+1
    What should I use here to show 21
    This could be done:

    <!-- results will be displayed here -->
    <p id="one"></p>
    <p id="two"></p>
    <p id="three"></p>

    <script type="text/javascript">
    var one = document.getElementById('one');
    var two = document.getElementById('two');
    var three = document.getElementById('three');
    var count = 20;
    one.innerHTML = 'The initial value of count is: ' + count;
    var countNow = count + 1;
    two.innerHTML = 'The value of count is now: ' + count;
    three.innerHTML = 'The value of countNow is: ' + countNow;
    </script>


    Kinda clumsy, but it clearly shows what is going on as well as showing the proper way to display things (as opposed to "document.write").
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov


  •  

    Tags for this Thread

    Posting Permissions

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