acess of a string first letter.

Discussion in 'Javascript' started by Sergio del Amo, Nov 1, 2003.

  1. 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.
     
    Sergio del Amo, Nov 1, 2003
    #1
    1. Advertising

  2. Sergio del Amo <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 1, 2003
    #2
    1. Advertising

  3. JRS: In article <>, seen in
    news:comp.lang.javascript, Lasse Reichstein Nielsen <>
    posted at Sat, 1 Nov 2003 19:33:11 :-
    >Sergio del Amo <> 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.
     
    Dr John Stockton, Nov 2, 2003
    #3
  4. 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
     
    Thomas 'PointedEars' Lahn, Nov 24, 2003
    #4
  5. JRS: In article <>, seen in
    news:comp.lang.javascript, Thomas 'PointedEars' Lahn
    <> 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.
     
    Dr John Stockton, Nov 24, 2003
    #5
  6. 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
     
    Thomas 'PointedEars' Lahn, Nov 25, 2003
    #6
  7. JRS: In article <>, seen in
    news:comp.lang.javascript, Thomas 'PointedEars' Lahn
    <> 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.
     
    Dr John Stockton, Nov 25, 2003
    #7
  8. 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
     
    Thomas 'PointedEars' Lahn, Nov 26, 2003
    #8
  9. JRS: In article <>, seen in
    news:comp.lang.javascript, Thomas 'PointedEars' Lahn
    <> 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.
     
    Dr John Stockton, Nov 26, 2003
    #9
  10. Dr John Stockton <> 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Nov 27, 2003
    #10
  11. JRS: In article <>, seen in
    news:comp.lang.javascript, Lasse Reichstein Nielsen <>
    posted at Thu, 27 Nov 2003 02:26:19 :-
    >Dr John Stockton <> 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.
     
    Dr John Stockton, Nov 27, 2003
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jay
    Replies:
    1
    Views:
    19,097
    Marc Dzaebel
    Feb 25, 2004
  2. f
    Replies:
    1
    Views:
    752
    Dimitre Novatchev
    Nov 27, 2003
  3. vertigo

    big letter -> small letter

    vertigo, Jul 6, 2004, in forum: Python
    Replies:
    4
    Views:
    774
    Reinhold Birkenfeld
    Jul 6, 2004
  4. Tony Meyer

    RE: big letter -> small letter

    Tony Meyer, Jul 6, 2004, in forum: Python
    Replies:
    0
    Views:
    511
    Tony Meyer
    Jul 6, 2004
  5. Andrew McNamara

    Re: big letter -> small letter

    Andrew McNamara, Jul 6, 2004, in forum: Python
    Replies:
    2
    Views:
    760
    Scott David Daniels
    Jul 6, 2004
Loading...

Share This Page