luiza oliveira
luiza oliveira

Reputation: 7

How to take div value through parent elements using jquery?

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

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

Upvotes: 0

Views: 33

Answers (2)

Archit Gargi
Archit Gargi

Reputation: 685

This code will access the text inside that div by accessing the div through its parent elements.

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 class="choices">
    <li>
      <div>UNIT01</div>
    </li>
    <li>
      <div>UNIT02</div>
    </li>
  </ul>
</div>

Upvotes: 0

Poncy
Poncy

Reputation: 21

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

Upvotes: 1

Related Questions