Friday, November 28, 2008

[jQuery] Re: Remove ellipsis from shortened title.

Your code is using … as part of a selector, which won't do what you
want. It will probably end up selecting nothing. And jQuery's remove()
method removes entire DOM nodes. You don't want that; you just want to
remove part of the text.

Also, you mentioned H3 elements, but the selector has a DT in it. Which is
it?

The code to do what you describe could look something like this:

$(function(){
$('h3').each( function() {
var $h = $(this);
$h.html( $h.html().replace( '…', '' ) );
});
});

Or, breaking it down step by step to make it more clear:

$(function(){
$('h3').each( function() { // loop through all H3 elements
var $h = $(this); // get the current H3 has a jQuery object
var html = $h.html(); // get the HTML content of the element
html = html.replace( '…', '' ); // remove '…'
$h.html( html ); // store the HTML back in the element
});
});

-Mike

> From: DemersDesigns
>
> Hello,
> I am using Simplepie and truncating fields that are longer
> than a certain length. This process adds an ellipsis to the
> end, which I want in most situations. However, for titles, I
> do not want the ellipsis to show. I am trying to use jquery
> to remove the ellipsis character code
> (…) from any H3 tag that has it, but am having a real
> problem getting it done. Here is what I have been working
> from with no luck.
>
> <script type="text/javascript">
> $(function(){
> $("dt &hellip;").remove();
> });
> </script>
>
> Thanks as always!
> -Paul

No comments: