Results 1 to 5 of 5

Thread: Trying to truncate a string in JQuery

  1. #1
    Join Date
    Jul 2008
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Trying to truncate a string in JQuery

    Hi,

    I have the following code:

    <div class="person">John S</div>

    Is is possible in JQuery to remove the last letter of the text within the div class "person"?

    I'm banging my head off a brick wall trying to figure this out, any help would be greatly appreciated.

    Thanks in advance

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Using the class attribute to select the element may not be the best method, but that depends upon a number of things, and can be fine in certain cases, also depending upon a number of things. Anyways:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.pack.js"></script>
    </head>
    <body>
    <div class="person">John S</div>
    <script type="text/javascript">
    $('.person').text($('.person').text().replace(/.$/, ''));
    </script>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2008
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Something strange is happening, I have 3 <div>'s with this class on it and after applying the code each div now contains the 3 people's names with only the last name with the surname removed. Any ideas on where I might be going wrong?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well, what did you expect to happen? Isn't this what you wanted?
    after applying the code each div now contains the 3 people's names with only the last name with the surname removed.
    Considering that 'last name' and 'surname' are synonyms, this is quite an interesting problem description. Very Zen.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I told you class was a tricky way to get the element. I think you want:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.pack.js"></script>
    </head>
    <body>
    <div class="person">John S</div>
    <div class="person">Bob Y</div>
    <div class="person">Sally Q</div>
    <script type="text/javascript">
    $.each( $('.person'), function(i, p){$(p).text($(p).text().replace(/.$/, ''))});
    </script>
    </body>
    </html>
    Though there is a more native each (specific to jQuery objects only) that might work better (faster):

    Code:
    $('.person').each(function(){$(this).text($(this).text().replace(/.$/, ''))});
    Last edited by jscheuer1; 11-21-2008 at 01:01 PM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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