<...">

Find div inside div with text

I have not pasted all the content here div

as it is too long

<div id="calendar_month">
    <div>
        <div></div>
        <div>DEMO</div>
    </div>
</div>

      

I tried this

 $("#calendar_month").children().each(function () {
     if ($(this).text() == "DEMO") {
         $(this).text("");
     }
 });

      

+3


source to share


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>
      

Run codeHide result




 

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>
      

Run codeHide result


+8


source


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("");
     }
 });

      



DEMO

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







All Articles