1

I'm indexing through a form with something like:

var i = 0;
var s = new String( i+'.prop');
var v = document.formName[s].value;

but what I get is actually:

document.formName[0].value

it appears that my index value "0.prop" is getting cast to an int, only in Internet Explorer (8)

any ideas on how to stop that?

UPDATE: here's a jsfiddle that exhibits the problem. Should return '111', but it returns foo in IE8.

3 Answers3

1

Use the toString() method:

var i = 0;
var s = i.toString() + '.prop';

typeof(s) === string // true
jlaceda
  • 866
  • 6
  • 11
1

FOUND IT! according to this answer NAME attributes must start with a letter not a number...

so adapting your html and using

var i = 0;
var s = 'prop_'+i;
var v = document.formName[s].value;

will work (I also updated the fiddle)

see fiddle

Community
  • 1
  • 1
Tobias Krogh
  • 3,768
  • 20
  • 14
  • http://jsfiddle.net/KPEv3/ works for me... or did I missed something regarding your question? – Tobias Krogh Apr 19 '12 at 17:38
  • curious, that works for me. I sliced what I thought was the problem code out of a bigger chunk. Let me go further up the chain. Thanks for your answer and follow up. – Jason Crowther Apr 19 '12 at 17:55
  • you could flag / vote this answer as an answer for this particular question, if you want to :) – Tobias Krogh Apr 19 '12 at 18:03
  • updated question to include a fiddle that exhibits the problem. +tobias, just needed some more elements in the form to exhibit the problem. – Jason Crowther Apr 19 '12 at 18:13
0

Do

var s = new String(''+i+'.prop');

Or

var s = new String( i.toString()+'.prop');
Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79