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;
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; }
Try this code: <input type="text" id="item7"> <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):
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