tags:

views:

105

answers:

4

I have a string, and I need to get its first character.

var x = 'somestring'
alert(x[0]); //in ie7 returns undefined

How can I fix my code?

+3  A: 

Hello Syom, try this:

x.substring(0,1)
ŁukaszW.pl
+14  A: 

What you want is charAt.

var x = 'some string';
alert(x.charAt(0)); // alerts 's'
Daniel Vandersluis
Worked on IE7, Chrome, Firefox.
Yuriy Faktorovich
It works in IE6 as well.
Daniel Vandersluis
+5  A: 

In JavaScript you can do this:

alert(x.substring(0,1));
Dustin Laine
+4  A: 
var x = "somestring"
alert(x.charAt(0));

The charAt() method allows you to specify the position of the character you want.

What you were trying to do is get the character at the position of an array "x", which is not defined as X is not an array.

Eton B.