firefox says NaN, IE works fine

Discussion in 'JavaScript' started by aras, Oct 29, 2007.

  1. #1
    I have an "add to cart" application where i increment a value in a input text and update the total number in an HTML area.

    Both work fine in IE. But in Firefox, input element updates fine, the html area updates NaN (not a number). the portions of updater code is here. Its pretty simple.


    -- input type --
    var num = document.getElementById(var1).value;
    document.getElementById(var1).value = parseInt(num) + 1;


    -- innerHTML --
    var num2 = document.getElementById(var2).innerHTML;
    var total = parseInt(num2) + 1;
    document.getElementById(var2).innerHTML = total;
     
    aras, Oct 29, 2007 IP
  2. dpfreaks

    dpfreaks Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    give some extra code, so that I can help you easily
     
    dpfreaks, Oct 29, 2007 IP
  3. aras

    aras Active Member

    Messages:
    533
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #3
    This is the all code basically. But i'm pasting again

    calling it like add('item7');

    there is
    <input type="text" id="item7"> <- updates on both IE,Firefox fine
    and
    <div id="totals">0</div> <- updates on IE, works on Firefox at the first increment, second increment it says NAN

    function add(var1) {
    var num = document.getElementById(var1).value;
    document.getElementById(var1).value = parseInt(num) + 1;

    var num2 = document.getElementById("totals").innerHTML;
    var totals = parseInt(num2) + 1;
    document.getElementById("totals").innerHTML = totals;
    }
     
    aras, Oct 29, 2007 IP
  4. dpfreaks

    dpfreaks Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try this code:

    <input type="text" id="item7">&nbsp;<input type="button" onClick="add('item7')" value="ADD"><BR>
    Total:<div id="totals">0</div>
    <script>
    function add(var1) {
    document.getElementById(var1).value = parseInt(document.getElementById(var1).value) + 1;
    document.getElementById('totals').innerHTML = parseInt(document.getElementById('totals').innerHTML) + 1;
    }
    </script>
    Code (markup):
     
    dpfreaks, Oct 29, 2007 IP
  5. aras

    aras Active Member

    Messages:
    533
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I figured out the problem. the div contains <b>NUM</b>, so Mozilla returns NAN on parseint(), however IE can parse the num out of the bold tags there. Wierd. Thank you anyway
     
    aras, Oct 29, 2007 IP
  6. dpfreaks

    dpfreaks Peon

    Messages:
    37
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Where is <b> & </b> tags?, Did you mention it?, If so use innerText instead of innerHTML
     
    dpfreaks, Oct 29, 2007 IP