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
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts

    jquery: remove the last letter

    Hi,

    I wonder how to remove the last letter inside an element,

    This is the code I came out with so far, but I cannot get the last letter from the structure of my elements and text - have I done something wrong in my HTML structure??

    Code:
    var str = $("#tag ul").text();
    var str = str.replace(/ /g, "");
    var the_length=str.length;
    var last = str.charAt(the_length-1);
    This is the HTML,

    Code:
    <div id="tag" class="tag">
                
                <h2><span>Behold, the handy tag system:</span></h2>
                
                <ul>
    
    			<a href="animation">animation</a> /
        		<a href="2-D">2-D</a> /
        		<a href="line-drawing">line-drawing</a> /
        		<a href="editorial">editorial</a> /
        		<a href="patterns">patterns</a> /
        		<a href="photomontage-floral">photomontage-floral</a> /
        		<a href="3-D">3-D</a> /
        		<a href="black-and-white">black-and-white</a> /
        		<a href="advertising">advertising</a> /
        		<a href="packaging">packaging</a> /
        		<a href="crests">crests</a> /
        		<a href="typography">typography</a> /
        		<a href="sketches">sketches</a> /
        		<a href="layered">layered</a> /
        		<a href="design">design</a> /
        		<a href="manga">manga</a> /
        		<a href="people">people</a> /
        		<a href="hand-lettered">hand-lettered</a> /
        		<a href="experimental">experimental</a> /
        		<a href="vector">vector</a> /
        </ul>
    I want to remove the last '/' (red) in the tag.

    this is the test site,

    http://lauthiamkok.net/works/design/...r_monkey_2010/

    Many thanks,
    L

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    My first thought is that you're doing it wrong.

    What's writing out that list of links? Are you looping through an array or something and writing a link for each item? If so, then the logic that strips the final slash off should surely be happening there, rather than writing it all out and then writing some additional code to amend the output once it's arrived on the page.

    If that's not the case, then...

    You need some <li> tags around your <a> tags. ie:

    Code:
    <ul id="tags">
    	<li><a href="animation">animation</a> /</li>
    	<li><a href="2-D">2-D</a> /</li>
    </ul>
    with that HTML, you could strip the final slash off the final list item with:

    Code:
    var lastItemHTML = $('#tags li:last').html();
    $('#tags li:last').html(lastItemHTML.substr(0,lastItemHTML.length-1));

  • #3
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Spudhead View Post
    My first thought is that you're doing it wrong.

    What's writing out that list of links?
    hello thank you for this!

    it's written in php to generate that list from the database.

    now I know i have the invalid html mark-ups!

    thank you for the help!


  •  

    Posting Permissions

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