homepage Welcome to WebmasterWorld Guest from 54.196.62.132
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
String to array problem in IE 7
vikas5678




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

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?

 

Arno_Adams




msg:4072647
 11:26 am on Feb 2, 2010 (gmt 0)

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?

daveVk




msg:4072653
 11:43 am on Feb 2, 2010 (gmt 0)


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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved