简体   繁体   中英

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>

Use :contains(TEXT) selector => Select all elements that contain the specified text.

$("div:contains('save jpeg')").on("click",function(){
  console.log(this.textContent);
});

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM