2

I'm trying to figure out how to remove the dash and colon here using jQuery. Is there a way? So far, I've tried .empty and .remove but I couldn't get it to work, unfortunately.

<p class="meta">
     <strong>Author Name</strong>
     –                                <-- remove this
     <time>June 7, 2013</time>
     :                                <-- remove this
</p>
Jesuraja
  • 3,774
  • 4
  • 24
  • 48
J82
  • 8,267
  • 23
  • 58
  • 87
  • possible duplicate of [jQuery: how to remove the text but not the children elements](http://stackoverflow.com/questions/2715167/jquery-how-to-remove-the-text-but-not-the-children-elements) – jpgauthier Jul 11 '14 at 06:24

6 Answers6

2

You could use...

$('.meta').contents().filter(function() { return this.nodeType == 3 }).remove()

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984
1

Try this :

$(function(){
   var $children = $('.meta').children();
   $('.meta').empty().append($children);
});

Demo

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
0

Try this,

$(function(){
   $('p.meta').html($('p.meta').find('*'));
});

Live Demo

Or try,

$(function(){
    $('p.meta').html(function(){ return $(this).find('*');});
});

Alternative

Updated, if there are events attached with your child element then use contents and filter like,

$(function(){
    $("p.meta").contents().filter(function(){ return this.nodeType == 3; }).remove();
});

Demo

Refer How can I change an element's text without changing its child elements?

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
0

I would suggest to re-create your $(".meta") DOM node, so you got to extract strong and time, empty your $(".meta") and insert previously stored nodes.

Aleksandr
  • 2,185
  • 2
  • 21
  • 29
0

You need to use contents(), that returns all children (including text nodes). Then use remove() to remove those text nodes.

http://jsfiddle.net/Pv3qX/

$(".meta").contents().filter(function() {
     return this.nodeType == 3; // 3 for TEXT_NODE
}).remove()
Jose Rui Santos
  • 15,009
  • 9
  • 58
  • 71
-1

Here is the solution: http://jsfiddle.net/E6GYs/

$(document).ready(function(){
    $(document).click(function(){
     $('.meta time').remove();
  })
    })
Maxim Ershov
  • 1,284
  • 1
  • 11
  • 15