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 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2005
    Location
    Pennsylvania
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    getElementById Method

    Any idea how I can get this display one number higher than it would display, i.e. if "which" is equal to 4 I want it to display 5. Any help will be appreciated.


    Code:
    function changepic(which)
    {
    var goodnum = which;
    document.img.src = pic[goodnum];
    document.getElementById('imgText').innerHTML = which; 
    }
    Code:
    Image <span id='imgText'>#</span> of 11

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If I understand your problem correctly, the problem is that "which" is a string and say 'which + 1' just concatinates '1' to the end of the string. You can quickly convert a string to a number by doing some arithmetic operation on it (aside from addition) without changing the actual value. Any of the following should work:

    1) (which *1) + 1
    2) (which / 1) + 1
    3) (which - 0) + 1
    4) (+which) + 1

  • #3
    New Coder
    Join Date
    Sep 2005
    Location
    Pennsylvania
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That did it. Thank you very much.

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    100
    Thanks
    0
    Thanked 0 Times in 0 Posts
    or just use eval()

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    The most appropriate way is by using Number(), parseInt() or parseFloat() method.

    Code:
    Number(which) + 1
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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