0
<div class="breadcrumbs">
    <a title="Go to Page." href="http://www.domain.com/dev" class="home">Home Page</a> &gt; <a title="Go to the News category archives." href="http://www.domain.com/dev"/topics/news/" class="taxonomy category">News</a> &gt; <span>News</span>
</div>

In the above example there are 2 seperators, I'm using the following jQuery('.breadcrumbs .category').remove(); to remove the link to the News but this leaves me with

<div class="breadcrumbs">
    <a title="Go to Page." href="http://www.domain.com/dev" class="home">Home Page</a> &gt; &gt; <span>News</span>
</div>

What would be the best way to remove one of the &gt;?

ngplayground
  • 20,365
  • 36
  • 94
  • 173

5 Answers5

5

Don't worry about removing the last occurrence, use css to place the (presentational) > in the right place:

.breadcrumbs a + a::before {
    content: '>';
}

Or, as the current page is a span you might prefer:

.breadcrumbs a::after {
    content: '>';
}
David Thomas
  • 249,100
  • 51
  • 377
  • 410
4

What would be the best way to remove one of the &gt;?

To fix the plugin that generates it (and the News link) in the first place.

What would be the easiest way to remove it via JavaScript?

This will do it:

jQuery('.breadcrumbs .category').each(function() {
    $([this, this.nextSibling]).remove();
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

My solution is the same as @David's, but using the single colon syntax, browser support, and removes the annoying link coloring and underlining (Doesn't make the > not a link though.

.breadcrumbs > a:after{
    content: '>';
    padding-left: 2px;
    display:inline-block;
}

.breadcrumbs > a:link:after{
    color: #000;
    text-decoration: none;
}

Fiddler

Justin
  • 3,337
  • 3
  • 16
  • 27
0

Update: I understood the problem better.

Update 2: Replacing with matched string, as Bergi suggested.

I came up with the following script that does what you need it to do:

jQuery(".breadcrumbs .category").remove();
var resultingHtml = jQuery(".breadcrumbs").html();
resultingHtml = resultingHtml.replace(/(\s*&gt;){2,}/mi, "$1");
jQuery(".breadcrumbs").html(resultingHtml);

What this script does is first remove the very last node in breadcrumbs, Then it replace any double (or more) &gt; text with a single gt;.

You can see it working on JSFiddle

pavlindrom
  • 366
  • 2
  • 12
0

Inspired from another question on stackoverflow

$.fn.nextNode = function(){ var contents = $(this).parent().contents(); return contents.get(contents.index(this)+1); } $('.breadcrumbs .category').nextNode().remove(); $('.breadcrumbs .category').remove();

Community
  • 1
  • 1
Corinne Kubler
  • 2,072
  • 5
  • 22
  • 34
  • Whoa, that hurts. `$.fn.nextNode = function(){ return this.map(function(){return this.nextSibling;}); };` – Bergi Jun 02 '14 at 14:53