Find div inside div with text
2 answers
You can use : contains ()
$( "#calendar_month div:contains('DEMO')" )
Or after editing your OP:
$( "#Calendar > div > div:contains('DEMO')" ).text("");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div id="Calendar">
<div>
<div>test</div>
<div>DEMO</div>
</div>
</div>
Or after @BhushanKawadkar comment, you can use . filter () :
$( "#Calendar > div > div" )
.filter(function( index ) {
return $(this).text() == "DEMO";
}).text("");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Calendar">
<div>
<div>test</div>
<div>DEMO</div>
</div>
</div>
+8
source to share
Yours div
containing the text DEMO is not a direct child div
with id calendar_month
. In your HTML, children()
only the first div will be returned.
Use find()
Try:
$("#calendar_month").find('div').each(function () {
if ($(this).text() == "DEMO") {
$(this).text("");
}
});
Or another way (not recommended, but just posting for logic) this particular markup is to use
$("#calendar_month").children().children().each(function () {
if ($(this).text() == "DEMO") {
$(this).text("");
}
});
+1
source to share