Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-09-2010, 05:27 PM   PM User | #1
lauthiamkok
Regular Coder

 
Join Date: Dec 2008
Posts: 117
Thanks: 14
Thanked 0 Times in 0 Posts
lauthiamkok is an unknown quantity at this point
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
lauthiamkok is offline   Reply With Quote
Old 02-10-2010, 03:25 PM   PM User | #2
Spudhead
Senior Coder

 
Spudhead's Avatar
 
Join Date: Jun 2002
Location: London, UK
Posts: 1,856
Thanks: 8
Thanked 110 Times in 109 Posts
Spudhead is on a distinguished road
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));
Spudhead is offline   Reply With Quote
Old 02-10-2010, 06:43 PM   PM User | #3
lauthiamkok
Regular Coder

 
Join Date: Dec 2008
Posts: 117
Thanks: 14
Thanked 0 Times in 0 Posts
lauthiamkok is an unknown quantity at this point
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!
lauthiamkok is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:28 PM.


Advertisement
Log in to turn off these ads.