click on a dynamically generated div - no ID and class, nested inside a static div using jquery

I want to call click function using JQUERY on the div containing text "Save as JPEG" . The div with ID= "graph1" is static and all other nested divs are dynamic. The dynamic div containing text has no class or ID.

<div id="graph1" class="col-sm-12" style="height: 250px">
    <div class="contianer">
        <div class="convascharttoolbar">
            <div>
                <div>save jpeg</div>
                <div>save png</div>
            </div>
        </div>
    </div>
</div>

Answers 1

  • Working Demo

    $(document).ready(function() {
      
      $("#graph1").on('click','div', function() {
        if($(this).text() == "save as jpeg"){
          alert('Div clicked')
        }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="graph1" class="col-sm-12" style="height: 250px">
      <div class="contianer">
        <div class="convascharttoolbar">
          <div>
            <div>save as jpeg</div>
            <div>save png</div>
          </div>
        </div>
      </div>
    </div>


Related Articles