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

    Join Date
    Jul 2004
    Rep Power

    Trying to truncate a string in JQuery


    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
    Rep Power
    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