total newbie question

G

Gordan

I have a form and a txtBox

why cant I do alert(document.form.txtBox.value[2])
I just want to print the third character of the value!
isn't that done by []?

please help :)
thanks, Gordan
 
L

Lasse Reichstein Nielsen

Gordan said:
I have a form and a txtBox
why cant I do alert(document.form.txtBox.value[2])

I recommend using
document.forms['form'].elements['txtBox'].value ...
It's probably not the problem though.
I just want to print the third character of the value!
isn't that done by []?

No. It worked in Netscape 4, but it is not the correct way to get a
character of a string.

Use
... .value.charAt(2);

/L
 
E

E620

Try:

document.form.txtBox.value.substr(2, 1);

As for why .value[2] does not work... I might be a little off here, but I
think the general idea is that a string is not so much a simple array of
chars as it is an object, one based on (or of) the string class.

Still kinda newbie myself at oop.
 
L

Lasse Reichstein Nielsen

(Please don't top post)
Try:

document.form.txtBox.value.substr(2, 1);

That will work too, as will .substring(2,3)
(I hope you picked the right one, I can never remember which is which :)
As for why .value[2] does not work... I might be a little off here, but I
think the general idea is that a string is not so much a simple array of
chars as it is an object, one based on (or of) the string class.

A little off, yes, and a little on too.

Strings in Javascript are primitive values. It's one of the five types of
non-object values (the other being number, boolean, null and undefined).
Strings are immutable and have no properties. Not even "length".

What happnes when you write
"foo".length
is that the Javascript interpreter sees a property lookup. It then
converts the value before the "." to an object. Primitive values are
converted to objects using the appropriate constructor (String, Boolean
or Number, null and undefined cannot be converted to objects and gives
an error).

So, the above is equivalent to
new String("foo").length

The String object (or rather its prototype) has methods like "charAt",
"substr", and "split". It also has the property "length" which is set
to the length of the string used to create the object.

So, strings are not arrays of chars. In Netscape 4, the String object
could be accessed using integer indices, but later browsers dropped
that. Strings are not instances of the string class, but they are
converted to such if needed.


/L
 
M

Michael Winter

Lasse Reichstein Nielsen wrote on 27 Nov 2003:

That will work too, as will .substring(2,3)
(I hope you picked the right one, I can never remember which is
which :)

"E620" did pick the right one:

String.substr( i, n )

Returns 'n' characters from the i'th index onwards

String.substring( i, j )

Returns all characters from the i'th index up to (but not including)
the j'th index. It is the equivalent of:

String.substr( i, j - i )

Ring any bells?

Mike
 
L

Lasse Reichstein Nielsen

Michael Winter said:
String.substring( i, j ) ....
It is the equivalent of:

String.substr( i, j - i )

Ring any bells?

Hey, that's a way to remember it! The shorter method is the one that
takes the lower second argument, when you want them to get the same
substring. :)

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top