#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    211
    Rep Power
    11

    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
  3. Standing 3 feet to the left.
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2006
    Location
    USA
    Posts
    1,435
    Rep Power
    186
    You don't need jQuery to do this. Javascript substring method. I'm not familiar with jQuery, but use their text-editing functions to get the text, apply that, then return it. (Looks like html() and replaceWith(), maybe.)

    Now, are you trying to just remove the last letter of anything, or are you trying to remove the initial for the person's last name? If you only expect a last initial to be entered, then you can safely stick with just substring(). If, however, you are worried also about full last names, you'll have to turn to split() and use the first part of the returned array, or, worst case, use regex.
    I'd rather teach you to fish than give you a fish. I reserve the right to also slap you with the fish.

IMN logo majestic logo threadwatch logo seochat tools logo