In JavaScript, how to get the length of a string?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: In JavaScript, how to get the length of a string?

  1. #1
    Join Date
    May 2005
    Posts
    36

    Angry In JavaScript, how to get the length of a string?

    In JavaScript, how to get the length of a string?

  2. #2
    Join Date
    Aug 2005
    Posts
    2

    Re: In JavaScript, how to get the length of a string?

    have you tried .length ?

  3. #3
    Join Date
    May 2005
    Posts
    36

    Angry Re: In JavaScript, how to get the length of a string?

    <TD>Note:</TD>
    <TD>
    <TEXTAREA NAME="note" ROWS="5" COLS="35"></TEXTAREA>
    </TD>

    // Nothing happens
    alert( form.note.value.length() );

  4. #4
    Join Date
    May 2004
    Location
    MN / NJ, United States
    Posts
    768

    Re: In JavaScript, how to get the length of a string?

    Quote Originally Posted by _mynameisno1
    alert( form.note.value.length() );
    You want:
    Code:
    alert(form.note.value.length);
    Length is not a method of the string attribute, the reason why the parentheses are avoided.
    *9-11-01* Never Forget; Never Forgive; Never Relent!
    "If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan

  5. #5
    Join Date
    May 2005
    Posts
    36

    Smile Re: In JavaScript, how to get the length of a string?

    oh, ha, thank you!

  6. #6
    Join Date
    Aug 2005
    Location
    Canada
    Posts
    7

    Re: In JavaScript, how to get the length of a string?

    Assuming it is in a form:

    alert(document.forms[0].note.value.length)

    If it is not in a form then use id="note" instead of name="note"

    And get it with:

    alert(document.getElementById('note').value.length)
    or I suppuse you might get it inside the tag with:

    alert(document.getElementById('note').innerHTML.length)

    Though I'm not sure it should be there based on the standards.
    _____________________________________________

    Home is a place where, when you go there, they have to take you in. (Robert Frost)

    Some of us have no Home. (COBOLdinosaur)

    Cd&

  7. #7
    Join Date
    May 2004
    Location
    MN / NJ, United States
    Posts
    768

    Re: In JavaScript, how to get the length of a string?

    Quote Originally Posted by COBOLdinosaur
    Though I'm not sure it should be there based on the standards.
    I'd stick away from innerHTML. It is simply a Microsoft corruption of DOM. Instead, one could usually use .firstChild.date. However, if it is an input element (text box), the value attribute is best for use.

    If you hae name="note", and that is froma databse, and unchangeable, the way to do that would be to use .getElementsByName('note')[0].

    document.getElementsByName('') returns an HTMLCollection of elements with that name. 0 refers to the first (and only, probably) element with the name note.
    *9-11-01* Never Forget; Never Forgive; Never Relent!
    "If we ever forget that we're one nation under God, then we will be a nation gone under." - Ronald Reagan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center