acess of a string first letter.

S

Sergio del Amo

Hi,
I am trying to create a web site to work in Opera 7.2, Explorer 6.0 and
Mozilla 1.4.
I have the code:

var imgs= document.getElementsByTagName("img");
alert(imgs[1].id);

The id of imgs[1] is a10. A window is open with Opera, Explorer and
Mozilla with the text "a10". When i write:

var imgs= document.getElementsByTagName("img");
alert(imgs[1].id[0]);

A window is open in Mozilla with the text "a". But in the other two with
the text "undefined".

Does anybody knows if there is a way to acces the first letter of a
string(in my case the string which contains the id name) with Explorer,
Mozilla and Opera??.
Thanks for your future feedback.
 
L

Lasse Reichstein Nielsen

Sergio del Amo said:
Does anybody knows if there is a way to acces the first letter of a
string

string.charAt(0)
or
string.substring(0,1)
or
string.substr(0,1)

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
string.charAt(0)
or
string.substring(0,1)
or
string.substr(0,1)

And for the question as quoted : those access the first character,
assuming that there is one.

S = '123go'
OK = /([a-z])/i.test(S)
RESULT = RegExp.$1

gives me 'g'. A larger search term will be needed for letters outside
A-Z.

What's Result for in JS?!?
 
T

Thomas 'PointedEars' Lahn

Dr said:
S = '123go'
OK = /([a-z])/i.test(S)
RESULT = RegExp.$1

RegExp.$x is deprecated in JavaScript 1.5. Use RegExp.exec(...) or
String.match(...) and the elements of the resulting array instead:

var
s = "123go",
result = /[a-z]/i.exec(s),
ok = result.length > 0;
What's Result for in JS?!?

?


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Thomas 'PointedEars' Lahn
Dr said:
S = '123go'
OK = /([a-z])/i.test(S)
RESULT = RegExp.$1

RegExp.$x is deprecated in JavaScript 1.5. Use RegExp.exec(...) or
String.match(...) and the elements of the resulting array instead:

RegExp.exec() is buggy, according to Flanagan, in my system. Those for
whom deprecated is inadequate can change it.
 
T

Thomas 'PointedEars' Lahn

Dr said:
Thomas 'PointedEars' Lahn
RegExp.$x is deprecated in JavaScript 1.5. Use RegExp.exec(...) or
String.match(...) and the elements of the resulting array instead:

RegExp.exec() is buggy, according to Flanagan, in my system. [...]

What is the bug, what is your system, and does the same go for
String.match(...)?


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Thomas 'PointedEars' Lahn
Dr said:
Thomas 'PointedEars' Lahn
RegExp.$x is deprecated in JavaScript 1.5. Use RegExp.exec(...) or
String.match(...) and the elements of the resulting array instead:

RegExp.exec() is buggy, according to Flanagan, in my system. [...]

What is the bug, what is your system, and does the same go for
String.match(...)?

If I had had more information, I would (probably) have given it.

Flanagan (ISBN 1-56592-521-1) p.74 says "Buggy in IE 4".

It makes no corresponding remark for String.match

Feel free to present your solution for finding the first letter of a
string, avoiding both buggy and deprecated features.
 
T

Thomas 'PointedEars' Lahn

Dr said:
Thomas said:
Dr said:
Thomas 'PointedEars' Lahn
RegExp.$x is deprecated in JavaScript 1.5. Use RegExp.exec(...) or
String.match(...) and the elements of the resulting array instead:

RegExp.exec() is buggy, according to Flanagan, in my system. [...]
[...]

Flanagan (ISBN 1-56592-521-1) p.74 says "Buggy in IE 4".

It makes no corresponding remark for String.match

Feel free to present your solution for finding the first letter of a
string, avoiding both buggy and deprecated features.

var
s = "123go",
result = s.match(/[a-z]/i),
ok = result.length > 0;


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Thomas 'PointedEars' Lahn
Feel free to present your solution for finding the first letter of a
string, avoiding both buggy and deprecated features.

var
s = "123go",
result = s.match(/[a-z]/i),
ok = result.length > 0;

For me, that sets ok to true. So far, so good. But removing the
letters from the string s brings up an Internet explorer script error,
result.length is not an object.

ok = result && result.length > 0 // is a bit better
ok = !!result && result.length > 0 // is satisfactory
 
L

Lasse Reichstein Nielsen

Dr John Stockton said:
For me, that sets ok to true. So far, so good. But removing the
letters from the string s brings up an Internet explorer script error,
result.length is not an object.

Yep. If there is no match, the result of calling the match method is
null.
ok = result && result.length > 0 // is a bit better
ok = !!result && result.length > 0 // is satisfactory

The second is only better for stylistic reasons.
If "result" is null, then the first line sets "ok" to null, and the
second sets it to false. Otherwise they are equivalent.

If you plan to use "ok" only in tests, there is no need to convert
it to a boolean. The important thing is that it is a false value.

/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Yep. If there is no match, the result of calling the match method is
null.


The second is only better for stylistic reasons.
If "result" is null, then the first line sets "ok" to null, and the
second sets it to false. Otherwise they are equivalent.

If you plan to use "ok" only in tests, there is no need to convert
it to a boolean. The important thing is that it is a false value.

True; but sometimes the plans change, and it is then be safer to have
what is a boolean in principle also be a boolean in practice.

Otherwise, at least, one may at a later date be surprised by the result
of a probing alert(ok).

My jt.htm seems to have stabilised ...
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top