Welcome to WebmasterWorld Guest from 50.16.68.229

Forum Moderators: open

String to array problem in IE 7

   
7:57 am on Feb 2, 2010 (gmt 0)

5+ Year Member



Hi guys,
I'm running into a really weird problem, I have a float number, which I am converting to string, and then trying to set each string character as the value of a html label.

It works on all browsers except that when I try it in IE 7, the array elements are "undefined".

Here's my code:

function increasePrice() {
var dollars= document.getElementById('dollarValue').firstChild.nodeValue;
var cents1 = document.getElementById('centValue1').firstChild.nodeValue;
var cents2 = document.getElementById('centValue2').firstChild.nodeValue;

var value = parseFloat(dollars +"." + cents1 + cents2) + 0.25;

var val = value.toString();

document.getElementById('dollarValue').firstChild.nodeValue = val[0];
document.getElementById('centValue1').firstChild.nodeValue = val[2];
document.getElementById('centValue2').firstChild.nodeValue = val[3];


}




Can someone please tell me what I am doing wrong?
11:26 am on Feb 2, 2010 (gmt 0)

10+ Year Member



Hmm, where did you create the array?

You convert the integer/float to string. Then you treat it like an array. Did you leave something out?
11:43 am on Feb 2, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member




document.getElementById('dollarValue').firstChild.nodeValue = val[0];
document.getElementById('centValue1').firstChild.nodeValue = val[2];
document.getElementById('centValue2').firstChild.nodeValue = val[3];

Never knew you could get the characters of a string that way ? In any case looks fragile eg 2.75 + 0.25 = 3 ( no cents in string )

try same maths to get 3 parts ( d,c1,c2 )

var d = Math.floor(val);
var cents = (val-d)* 100;
var c1 = Math.floor( cents/10 );
var c2 = cents - (c1*10);
 

Featured Threads

Hot Threads This Week

Hot Threads This Month