By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,652 Members | 1,441 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,652 IT Pros & Developers. It's quick & easy.

acess of a string first letter.

P: n/a
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.

--
Sergio del Amo.

Jul 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Sergio del Amo <se***@sbox.tugraz.at> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
JRS: In article <fz**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Sat, 1 Nov 2003 19:33:11 :-
Sergio del Amo <se***@sbox.tugraz.at> writes:
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)


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?!?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3

P: n/a
Dr John Stockton wrote:
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
Jul 20 '05 #4

P: n/a
JRS: In article <3F**************@PointedEars.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Mon, 24 Nov 2003 04:57:03 :-
Dr John Stockton wrote:
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5

P: n/a
Dr John Stockton wrote:
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
Jul 20 '05 #6

P: n/a
JRS: In article <3F**************@PointedEars.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Tue, 25 Nov 2003 13:25:23 :-
Dr John Stockton wrote:
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #7

P: n/a
Dr John Stockton wrote:
Thomas 'PointedEars' Lahn wrote:
Dr John Stockton wrote:
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
Jul 20 '05 #8

P: n/a
JRS: In article <3F**************@PointedEars.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Wed, 26 Nov 2003 17:19:52 :-
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

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #9

P: n/a
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10

P: n/a
JRS: In article <y8**********@hotpop.com>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <lr*@hotpop.com>
posted at Thu, 27 Nov 2003 02:26:19 :-
Dr John Stockton <sp**@merlyn.demon.co.uk> writes:
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.


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 ...

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.