Connecting Tech Pros Worldwide Forums | Help | Site Map

split() function is acting up!

Newbie
 
Join Date: Jun 2007
Posts: 21
#1: Jul 31 '07
i have some JavaScript code that gets a variable from an input field, then parses it into a float. That all goes OK, but after that it gets weird: i put it variable.split("."); and FireBug gives me this error:
Expand|Select|Wrap|Line Numbers
  1. timeSpanFull.split is not a function
Here is a snippet of the code:
Expand|Select|Wrap|Line Numbers
  1. timeSpanFull = parseFloat(fetchById('timeSpan').value);
  2. timeSpan = timeSpanFull.split(".");
I've looked at a few examples online, and cross checked with those, but came up with nothing. I also looked for hidden characters, but came up with nothing again.

Thanks in advance,
Naurus

gits's Avatar
Moderator
 
Join Date: May 2007
Location: Munich, Germany
Posts: 4,136
#2: Jul 31 '07

re: split() function is acting up!


hi ...

when using the split-function the value must be a string-value ... so you cannot use split() with a 'number-type'. but you can use:

Expand|Select|Wrap|Line Numbers
  1. // floating number
  2. var number = 2.3;
  3.  
  4. // split the parts of number
  5. var splitted_number_array = new String(number).split('.');
kind regards
Newbie
 
Join Date: Jun 2007
Posts: 21
#3: Jul 31 '07

re: split() function is acting up!


Thank you so much! i really wish that some of the online tutorials would've said that.

Quote:

Originally Posted by gits

hi ...

when using the split-function the value must be a string-value ... so you cannot use split() with a 'number-type'. but you can use:

Expand|Select|Wrap|Line Numbers
  1. // floating number
  2. var number = 2.3;
  3.  
  4. // split the parts of number
  5. var splitted_number_array = new String(number).split('.');
kind regards

Reply


Similar JavaScript / Ajax / DHTML bytes