views:

488

answers:

5

Hi Guys,

I have the following code:

$('.breadcrumb:contains(",")').hide();

It does as expected and hides the entire breadcrumb, but how would I go about just remove the comma?

========== EDIT:

<div class="breadcrumb">
   <a href="#">Link 1</a>,
   <a href="#">Link 2</a>
</div>

==========

Thanks for any help

A: 

.hide() hides the selected node. You're going to have to get the text from the node (using .text()), parse it to pull out the comma, and then re-set the text on the node.

Stefan Kendall
Alternatively, place spans around the commas and just hide them if you don't want to lose data.
Stefan Kendall
@Stefan, it is auto generated, this is why is has to dynamically done this way
Keith Donegan
+1  A: 
$('.breadcrumb:contains(",")').html($('.breadcrumb:contains(",")')
    .html().replaceAll(",", ""));
stevedbrown
+4  A: 

You can do it like this:

$('.breadcrumb:contains(",")').html(function(i, h) {
  return h.replace(/,/g, '');
});

The .html() function accepts a function, you can see the docs here. The above code replaces all commas in all .breadcrumb elements. Example here: http://jsfiddle.net/ZZG8p/

Nick Craver
I was ready to submit the same thing :P: Edit: Infact not quite, "h" is the current innerHTML, "this" is the DOM element. Do you not mean `return h.replace(/,/g, '');`
Matt
Thank you for your help Nick, unfortunately this removes everything from the breadcrumb div :|
Keith Donegan
@Matt - Saw that when I was testing just now, thanks :)
Nick Craver
@Keith Donegan - Just updated the answer, try once more.
Nick Craver
Just tried, same story I'm afraid, thanks anywho!
Keith Donegan
@Keith - Go to the jsfiddle link in my answer, this works...are you wrapping it inside a `document.ready`?
Nick Craver
Ah, it seems to be JQuery lib version, thanks Nick!!!
Keith Donegan
@Keith - Welcome :)
Nick Craver
A: 

You can JavaScript's String replace method. However, I'm going to go out on a limb and imagine what happened. You had and an array and then you looped through it to generate your bread crumbs and now you want to get rid of the trailing comma.

The solution to that problem is to use split() and use ',' as the separator. This way you only have internal commas.

easement
A: 
$('.breadcrumb:contains(",")').remove();

Though this will remove it from the DOM, so make sure you select the correct one.

Shervin