2

In below code, I want to hide the "Lorem Ipsum is a dummy content" by showing the other elements inside the parent container, either by using jquery, javascript or CSS? Solution much appreciated.

 <div class="someWrapper">
     Lorem Ipsum is a dummy content
      <div class="anotherContainer">
          <p>Text goes here</p>
      </div>
 </div>
Nitesh
  • 474
  • 3
  • 11

3 Answers3

7

To do that you need to target the textNode of the .someWrapper element and remove them from the DOM. To do that you can use filter() and remove(). Try this:

$('.someWrapper').contents().filter(function() {
  return this.nodeType == Node.TEXT_NODE; // 3
}).remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="someWrapper">
  Lorem Ipsum is a dummy content
  <div class="anotherContainer">
    <p>Text goes here</p>
  </div>
</div>
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

You can do this with a one liner, either with plain JavaScript:

document.getElementsByClassName('someWrapper')[0].firstChild.remove();

jsFiddle example

or jQuery:

$($('.someWrapper').get(0).firstChild).remove();

jsFiddle example

j08691
  • 204,283
  • 31
  • 260
  • 272
0

You can select inner content, empty outer element and append inner ounce again:

$(document).ready(function(){
  $outer = $('.someWrapper');
  $inner = $('.anotherContainer');
  $outer.empty();
  $outer.html($inner);
});

here is fiddle: https://jsfiddle.net/woo3cgwx/

Everettss
  • 15,475
  • 9
  • 72
  • 98
  • Your solution would work only in this particular case, i.e. it would not work if in `anotherContainer`, before `

    Text goes here

    ` there were another text node without a wrapper.
    – Alessio Cantarella Jan 25 '16 at 20:46
  • @AlessioCantarella You are right. I want to show the most intuitive and easy way to do it. – Everettss Jan 25 '16 at 20:53