Reputation: 595
I am having dom object with html as below:
<div class="parent">
this is my <span>11</span> original text i <span>23</span> want
<div class="child1">Lorem Ipsum is simply dummy text</div>
<div class="child2">Lorem Ipsum is simply dummy text</div>
</div>
Basically I want to remove all child divs not span by jquery. I tried,
myDom.find(".parent").immediateText()
But above code return only text not span text that I wanted. I want the result as below :
this is my 11 original text i 23 want
Upvotes: 0
Views: 58
Reputation: 642
$(".parent > [class^=child]").remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
this is my <span>11</span> original text i <span>23</span> want
<div class="child1">Lorem Ipsum is simply dummy text</div>
<div class="child2">Lorem Ipsum is simply dummy text</div>
</div>
Upvotes: 1
Reputation: 11055
If you want to select the span text:
$(".parent span").text();
or
$(".parent").children("span").text();
If you want to select others except the span (and perhaps hide them):
$(".parent").find("div") //.hide() or .remove()
Upvotes: 0
Reputation: 30739
Just use .parent div
Jquery selector to select all the div
inside element having class parent
and then use remove()
to remove them from DOM:
$(document).ready(function(){
$(".parent div").remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
this is my <span>11</span> original text i <span>23</span> want
<div class="child1">Lorem Ipsum is simply dummy text</div>
<div class="child2">Lorem Ipsum is simply dummy text</div>
</div>
Upvotes: 1
Reputation: 1736
Use :not(span) selector to select everything which is not span.
$('.parent').find(':not(span)').remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
this is my <span>11</span> original text i <span>23</span> want
<div class="child1">Lorem Ipsum is simply dummy text</div>
<div class="child2">Lorem Ipsum is simply dummy text</div>
</div>
Upvotes: 1
Reputation: 22323
Find div inside parent class and remove.
$('.parent').find('div').remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
this is my <span>11</span> original text i <span>23</span> want
<div class="child1">Lorem Ipsum is simply dummy text</div>
<div class="child2">Lorem Ipsum is simply dummy text</div>
</div>
OR:
$(document).find('.parent > div').remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="parent">
this is my <span>11</span> original text i <span>23</span> want
<div class="child1">Lorem Ipsum is simply dummy text</div>
<div class="child2">Lorem Ipsum is simply dummy text</div>
</div>
Upvotes: 1