2

I have html written on a page like this that I can not control and I can only get access to it using jquery, which I need your help to solve.

<span class="breadcrumb">
<a href="http://www.example.com" class="breadcrumb"></a>Home / 
<a href="http://www.example.com" class="breadcrumb">Home</a> / 
</span>

I would like to hide only the "Home /" text from the first line that has the empty link. This issue is the text is not wrapped in the link, but is plain text.

But I want to keep the second line visible that contains the link

<a href="http://www.example2.com" class="breadcrumb">Home</a> /

5 Answers 5

3

You can get the .contents().. then use .slice() to get the text portion that you don't want

$('span.breadcrumb').contents().slice(2,3).remove();

http://jsfiddle.net/3gpgY/

if you want to remove the anchor before the text too, just change the starting index

$('span.breadcrumb').contents().slice(1,3).remove();

http://jsfiddle.net/eK7F7/

Sign up to request clarification or add additional context in comments.

1 Comment

beautiful. that's what i needed $('span.breadcrumb').contents().slice(1,2).remove();
0

Try this:

$(".breadcrumb a:nth-child(2)").prevAll().remove();

Comments

0

Do the following :

var content = $('.breadcrumb a').eq(1);
$('.breadcrumb').eq(0).html(content);

You can see the result here : http://codepen.io/joe/pen/uoeIh

Comments

0

Here's a dirty solution

 $( '.breadcrumb' ).html( $( '.breadcrumb' ).html().replace( /[^\n]*><\/a>[^\n]*\n/, '' ) );

if you want to do it with the DOM nodes you've to use $.( '.breadcrumb' ).contents()

Comments

0

Try this:

$(document).ready(function() {
    var newCont = $('<span>')
    $('.breadcrumb a').each(function (){
        $(newCont).append($(this).html())
    })
    $('.breadcrumb').html(newCont)
})​

jsFiddle: http://jsfiddle.net/hA6gf/2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.