473,583 Members | 3,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

acess of a string first letter.

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.getEle mentsByTagName( "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.getEle mentsByTagName( "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
10 6153
Sergio del Amo <se***@sbox.tug raz.at> writes:
Does anybody knows if there is a way to acces the first letter of a
string


string.charAt(0 )
or
string.substrin g(0,1)
or
string.substr(0 ,1)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
JRS: In article <fz**********@h otpop.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.tug raz.at> writes:
Does anybody knows if there is a way to acces the first letter of a
string


string.charAt(0 )
or
string.substrin g(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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #3
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
JRS: In article <3F************ **@PointedEars. de>, seen in
news:comp.lang. javascript, Thomas 'PointedEars' Lahn
<Po*********@we b.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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5
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
JRS: In article <3F************ **@PointedEars. de>, seen in
news:comp.lang. javascript, Thomas 'PointedEars' Lahn
<Po*********@we b.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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #7
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
JRS: In article <3F************ **@PointedEars. de>, seen in
news:comp.lang. javascript, Thomas 'PointedEars' Lahn
<Po*********@we b.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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #9
Dr John Stockton <sp**@merlyn.de mon.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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
6012
by: Alan J. Flavell | last post by:
OK, today's email brought a comment from a reader, pointing out something that I'd long since noticed myself but hadn't done anything about it. On my pages, I've got a first-letter style on paragraphs. So far, so good: some of the paragraphs begin with a link, and there are styles in the stylesheet for link, hover etc. The first letter...
3
17090
by: jl_post | last post by:
Hi, I recently wrote two benchmark programs that compared if two strings were equal: one was a C program that used C char arrays with strcmp(), and the other was a C++ program that used std::strings with operator==(). In both programs, the first string consisted of one million characters (all the letter 'a'). The second string was...
6
7085
by: Alan | last post by:
I want to search a string in a file, but I don't know the position, how can I search the string and stop at that line containing that string ? for example, I want to seach the string ".ABC" in a file, how to do ? thx
1
3254
by: The_Kingpin | last post by:
Hi all, I need to make a function that convert a string into a certain format. Here what are the restriction: -The first letter of the first and last name must be uppercase. -If a first name contains only 1 character, a '.' must follow the char. -If we find a character that isn't a letter (.&*-), we must swap it for a '/' and add the...
18
7925
by: Marcio Kleemann | last post by:
I need to force the first letter of each word in a line of text to uppercase. The text comes from a TextBox control in a Web Form. I'm new to ..NET and am having a problem. Since I can't modify the string directly, I convert the string to a char using .ToCharArray() and set the first letter of each word to uppercase. The problem then is I...
53
4042
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
6
3414
by: James | last post by:
Good Evening, I would like to insert a 5 letter word into a 100 letter string, but only 1 letter at a time, and each letter separated by 10 characters!!! :) Using Alpha characters only, no numbers or punctuation in either string. The first letter needs to use the DAY OF THE MONTH number as its insertion point... So the string HELLO...
23
9492
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP will print literally as {{-Hello}} World, instead of
1
1899
by: lokeshreddy16 | last post by:
'this is my code plz guys help how to save data from vb 2005 and other this that i am able to view the data from acess but i am not able save to acess i dont whether my code for save is correct of not ( iam using vb2005 as front end and MS acess as by back end) Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal...
0
7896
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8329
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8197
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6583
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5377
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3821
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2335
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1436
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1160
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.