1

I hide the category tag "Featured" by using remove() method so it is hidden now I also want to hide the comma from here with jquery but I couldn't. I used remove() method for that but it is hiding the next link also. so please help me someone. Thanks.

<P class="cat">
<a href="#" >Featured</a>
, 

<a href="#">Others</a>
</P>
Exploring
  • 573
  • 2
  • 6
  • 19

1 Answers1

1

Using jQuery select and wrap textNode

$('p').contents()
    .filter(function(){return this.nodeType === 3})
    .wrap('<span />');

I eventually came up with this

Live Demo

// select and wrap all commas
$('.cat')
  .contents()
  .filter(function() {
    return this.nodeType === 3 && $.trim(this.nodeValue)==",";
  })
  .wrap("<span class='comma' />");

// hide all links containing "Featured" as innerHTML
$(".cat").find('a:contains("Featured")').hide();

// select all visible elements
var $coll = $(".cat").children(":visible");
$coll.each(function() {
    var $this = $(this);
    if ($this.prop("tagName")=="SPAN") { 
      if ($coll.index(this)===0 || 
          $this.nextAll(":visible").prop("tagName")=="SPAN") {
        $(this).hide();
      }
    }
});
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • I have used it and it is working. But one point is that if there comes more than one category names then will the comma appear or not? – Exploring Dec 08 '13 at 08:37
  • Great, Thank you very much. But I requested you to remove the site links from the fiddle. – Exploring Dec 08 '13 at 17:33
  • Thanks. I used the second one but it is making a little problem. It is hiding the next comma after "featured" as we used `next()` but if the comma becomes before "featured" name then it is not hiding. I update your jsfiddle, check it: http://jsfiddle.net/R8PJx/11/ – Exploring Dec 09 '13 at 04:34
  • My problem is solved but I am just exploring the problem. I used prev() and next() both here. But it is making problem. To understand more you can check this fiddle please: http://jsfiddle.net/R8PJx/17/ – Exploring Dec 09 '13 at 14:25
  • Please see update. I have requested the question to be opened again since it was more complicated than just wrapping – mplungjan Dec 09 '13 at 15:54
  • 1
    Yes, It is working awesome. Here is the fiddle: http://jsfiddle.net/R8PJx/18/ . Thanks. I think we should close the problem here. – Exploring Dec 10 '13 at 04:36