javascript - How to take the inner text of id and assign it to a specific variable only for a specific inner text -
i have different pages populate div tag specific id based on id inner text: here different example of pages div:
<div id="id_name">text page1</div> <div id="id_name">text page2</div> <div id="id_name">text page3</div>
i want take inner text of second , fill variable.
i try use this:
if($("#id_name").length > 0 && $("#id_name").text() === "text page2") { site.text = $("#id_name").text(); }
- do not use same id multiple elements. see why bad thing have multiple html elements same id attribute?
- use classes instead
- you can use :contains()
if ($(".class_name:contains(text page2)").length > 0) { $('body').text("text page2"); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div class="class_name">text page1</div> <div class="class_name">text page2</div> <div class="class_name">text page3</div>
Comments
Post a Comment