tags:

views:

74

answers:

5

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 !

+1  A: 

Untested, but should work:

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

alert(text[text.length-1]);
David
it can be nodeType ==2, or 4 the number of anchors are different
Alexander Corotchi
@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.
Josh Stodola
A: 

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();
Peter Loron
That's not what he wants. He want's the `Projects Text`.
poke
A: 

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();
MillsJROSS
Accidently typed in HTML instead of HREF in the anchor tags. This is now fixed.
MillsJROSS
+1  A: 

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.

poke
but with this  > , I can do that ?
Alexander Corotchi
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.
poke
A: 

@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.

ghoppe