Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Code:
    $(document).ready(function()
    	{
    		var cls='person'; // Name of the class you are referring
    		$('.'+cls).text($('.'+cls).text().substring(0,$('.'+cls).text().length-1));
    	});
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    New Coder
    Join Date
    Sep 2004
    Posts
    96
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?


  •  

    Posting Permissions

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