1

I am looking to hide anything not in a <p> within a certain <div> - specifically anything before the first <p>. For example:

<div id="description">
TEXT I WANT REMOVED
<p>Keep this text</p>
<p>Keep this too</p>
</div>
Sam
  • 6,616
  • 8
  • 35
  • 64

3 Answers3

1

there isn't something like a text-node-selector in css, so the only thing that comes to my mind to solve this is setting font-size: 0; for the outer element and back to a normal value for the ps (although this feels kind of... dirty).

see it in action on jsfiddle

Community
  • 1
  • 1
oezi
  • 51,017
  • 10
  • 98
  • 115
  • It works well - but like you said, its not clean to have to reset the font-size on the inner element. – Sam Nov 30 '13 at 07:43
  • 1
    still better than using javascript (wich seems to be the only usefil alternative), in my opinion. – oezi Nov 30 '13 at 07:45
0

One option is to use visibility - but the problem is the space occupied by the other elements may not be properly collapsed

#description {
    visibility: collapse;
}
#description p{
    visibility: visible;
}

Demo: Fiddle

Another solution is

$('#description').contents().filter(function(){
    return this.nodeType == 3 && $.trim(this.nodeValue) != '';
}).wrap('<span />').parent().hide()

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

May be you could try jquery:

var html = $('#description p').map(function(){
    return $(this)[0].outerHTML;
}).get();

$('#description').html(html);

Demo Fiddle to try

Jai
  • 74,255
  • 12
  • 74
  • 103