PDA

View Full Version : Resolved parseInt() is still recognizing string as string only



JShor
06-20-2009, 04:41 PM
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: _________



<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 :)

clueful
06-20-2009, 10:30 PM
Try:

newChildVal = parseInt(document.getElementById('childTotal').value) + 1;

Better still:


if( !isNaN( newChildVal = parseInt( document.getElementById('childTotal' ).value ) ) )
newChildVal += 1;

Jesdisciple
06-20-2009, 10:53 PM
EDIT: Dern, I'm late.

:)
newChildVal = parseInt(document.getElementById('childTotal').value) + 1;

JShor
06-21-2009, 12:14 AM
Thanks! It worked. Dumb me... :)