tags:

views:

42

answers:

1

I'm rendering an RSS feed and there is content at the beginning of each content section - it's always 6 characters and I want to use jQuery to remove it.

The content of the particular bit of feed is in a list tag, so <li>blahah Hello this is where I want to display from...</li>

Getting rid of the "blahah" is the goal.

An update:

The jQuery supplied works great but I can't work out why it's pulling all the list elements as if they're inline!

Would you be able to explain why the li tags in this example - jsfiddle.net/GhazG/5 run into each other when rendered, instead of appearing like this - jsfiddle.net/GhazG/6?

+6  A: 

This will remove the first 6 characters for every <li> element on the page. I imagine you'll want your selector to specifically target the <li> in question.

   // Select your <li> element
var $li = $('li');

   // Get the text(), and call .substr() passing the number 6 as the argument
   //   indicating that you want to get the part of the string starting on
   //   index number 6 (the seventh character)
$li.text( $li.text().substr(6) );

Try it out: http://jsfiddle.net/GhazG/

Since you have several <li> elements on the page that need updating, you should do so using an each loop:

Try it out: http://jsfiddle.net/GhazG/7/

$('li').each(function() {
    var $th = $(this);
    $th.text( $th.text().substr(6) );
});

If the items are being appended to the DOM via javascript, you would also have the option of removing the characters before you do the append. It would probably be a better approach.

patrick dw
In case it confuses the asker, there is no need to use $ in front of your variable names - removing it won't make a difference to the code running. It's purely naming convention preference.
jakeisonline
@jake is correct, though some would consider it to be highly recommended to reference your jQuery objects with variable names that start with `$`. It is a widely used convention and a nice reminder of what the variable references.
patrick dw
perfect, thank you!
Cordial
Ahh I've encountered an issue with what I'm trying to do. Would you be able to explain why the li tags in this example - http://jsfiddle.net/GhazG/5/ run into each other when rendered, instead of appearing like this - http://jsfiddle.net/GhazG/6/?Thanks!
Cordial
@Cordial - Absolutely. :o) You need to perform the action in a loop, so that you are only updating the content of each `<li>` with the current content. I'll update my answer. Here's an example: http://jsfiddle.net/GhazG/7/
patrick dw
fantastic, thanks so much. don't know what i'd do without you people! actually i'd probably be unemployed...
Cordial