Steve
Steve

Reputation: 14912

Remove part of a string inside a tag with jquery?

I have a page with links that look like this:

<a class="title" href="#">Recipes using <b>veal</b> - My Recipes</a>

I want to remove all the "- My Recipes" from these links, leaving (in this case):

Recipes using <b>veal</b>

I have tried many variations on $('a.title').replace( "- My Recipes", "" );

I think I must be targeting it wrong.

Upvotes: 1

Views: 108

Answers (2)

Kevin B
Kevin B

Reputation: 95024

The .html method can handle it:

$('a.title').html(function ( i, html ) {
    return html.replace( "- My Recipes", "" );
});

http://jsfiddle.net/QxbDK/2/

Probably better though to instead change what's returning this content so that it isn't there to begin with.

Upvotes: 1

Abraham Uribe
Abraham Uribe

Reputation: 3118

you can do:

$('a.title').each(function(){
    $(this).html($(this).html().replace( "- My Recipes", "" ));
});     

jsfiddle

Upvotes: 2

Related Questions