1

HI,

Can somebody help me with this :

This is my HTML:

    <div class="Breadcrumb">
       <a href="#">Home</a>&nbsp;&gt;&nbsp;
       <a href="#">Projects</a>&nbsp;&gt;&nbsp;
       Projects Text
    </div>

I want to get with jQuery the string what is not in anchor tag , in this example is "Projects Text"

Theoretically something like this

jQuery :

var name = $("Breadcrumb (not <a>) ").text();

but I don't need this one "&nbsp;&gt;&nbsp;"

Thanks !

5 Answers 5

1

Untested, but should work:

var text = $('.Breadcrumb').contents().filter(function() { 
    return this.nodeType == 3;
});

alert(text[text.length-1]);
Sign up to request clarification or add additional context in comments.

2 Comments

it can be nodeType ==2, or 4 the number of anchors are different
@Alexander Try the code. nodeType refers to the type of elements. 3 indicates a TEXT_NODE (try alert(Node.TEXT_NODE);) as opposed to an ELEMENT_NODE. And then he grabs the last elements in the array (the final block of text). You may need to strip out some of the markup, but this looks like a good solution to me.
1

That's not possible, as &nbsp;&gt;&nbsp; Projects Text is actually one node within the DOM tree. So you cannot get the Projects Text alone without filtering it out. If you know, that the text is preceded by &nbsp;&gt;&nbsp;, you could however simply split that out.

Or as MillsJROSS suggested, you need to put the code inside another tag.

2 Comments

but with this &nbsp;&gt;&nbsp;, I can do that ?
For example like this: $( '.Breadcrumb a:last' )[0].nextSibling.nodeValue – Don't know if there is a better way with jQuery, I don't use it that much.
0

This should give you the text of the "A" tags. You'll need to do some string parsing in javascript to snip out the nbsp bits. I suggest a regex.

var foo = $(".Breadcrumb a").text();

1 Comment

That's not what he wants. He want's the Projects Text.
0

I'd suggest changing the HTML to the following...

<div class="BreadCrumb">
    <a href="#">Home</a>&nbsp;&gt;&nbsp;
    <a href="#">Projects</a>&nbsp;&gt;&nbsp;
    <span class="your_class_name">Projects Text</span>
</div>

Then with Jquery you could just do the following...

var name = $(".BreadCrumb .your_class_name").text();

1 Comment

Accidently typed in HTML instead of HREF in the anchor tags. This is now fixed.
0

@poke is correct. Change your html:

<div class="Breadcrumb">
   <a href="#">Home</a>&nbsp;&gt;&nbsp;
   <a href="#">Projects</a>&nbsp;&gt;&nbsp;
   <span>Projects Text</span>
</div>

Javascript:

var name = $(".Breadcrumb span").text();

EDIT:

If you can't or won't change the html and your breadcrumbs are consistent, then you can use a regex to get rid of whitespace and anything before and including the >:

var name = $(".Breadcrumb").text().replace(/(^\s+|\s+$|(.*)>\s*)/g,"");

I still think, if you have control over the html generation, that adding the extra span tag is cleaner with the extra bonus of allowing you to easily style the current page with css.

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.