Home > Mobile >  How to take div value through parent elements using jquery?
How to take div value through parent elements using jquery?

Time:05-03

I need to take the text "UNIT01". How I can do this using JQuery or JS?

`<div id="unit">
 <ul >
   <li>
    <div>UNIT01</div>
   </li>
   <li>
    <div>UNIT02</div>
   </li>
 </ul>
</div>`

CodePudding user response:

This code will access the text inside that div -

var unit = document.getElementById("unit");
var list = unit.getElementsByTagName("ul")[0];
var items = list.getElementsByTagName("li")[0];
var div = items.getElementsByTagName("div")[0];
var text = div.innerHTML;
alert(text);
<div id="unit">
  <ul >
    <li>
      <div>UNIT01</div>
    </li>
    <li>
      <div>UNIT02</div>
    </li>
  </ul>
</div>

CodePudding user response:

$( document ).ready(function() {
var txt = $('.choices li:first-child>div').text()
console.log( txt );});
  • Related