Results 1 to 4 of 4

Thread: parseInt() is still recognizing string as string only

  1. #1
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default parseInt() is still recognizing string as string only

    When trying to select a string value from a hidden input, and adding 1 to it, it doesn't work. It recognizes it as a string, despite my attempts to use parseInt().

    For example, everytime I call the function addChild(), the innerhtml that's changed is the following:

    Child 1: _________
    Child 11: _________
    Child 111: _________
    Child 1111: _________

    Code:
    <script type="text/javascript">
    childDiv = document.getElementById('childDiv');
    
    function addChild() {
    newChildVal = parseInt(document.getElementById('childTotal').value + 1);
    document.getElementById('childTotal').value = newChildVal;
    
    document.getElementById('childDiv').innerHTML = document.getElementById('childDiv').innerHTML + 'Child ' + newChildVal + ' <input type="text" name="child[]" size="40"><br>';
    }
    
    </script>
    <div align="right" id="childDiv"></div>
    <input type="hidden" name="childTotal" id="childTotal" value="0">
    <a href="javascript:addChild()"><font color="Green">Add Child</font></a>
    Any help is appreciated
    Last edited by Snookerman; 06-21-2009 at 03:21 PM.
    - Josh

  2. #2
    Join Date
    Jul 2008
    Posts
    128
    Thanks
    0
    Thanked 17 Times in 16 Posts

    Default

    Try:
    Code:
    newChildVal = parseInt(document.getElementById('childTotal').value) + 1;
    Better still:
    Code:
    if( !isNaN( newChildVal = parseInt( document.getElementById('childTotal' ).value ) ) )
     newChildVal += 1;

  3. #3
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    EDIT: Dern, I'm late.

    Code:
    newChildVal = parseInt(document.getElementById('childTotal').value) + 1;
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  4. #4
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Thanks! It worked. Dumb me...
    - Josh

Bookmarks

Posting Permissions

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