Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  140] [ 3]  / answers: 1 / hits: 68993  / 11 Years ago, mon, august 12, 2013, 12:00:00

how can i get the Value / Text of a <li> item ?
I found on the internet much ways to get the value for a dropdown list.
But not for a <li> item.



This is what I have tried so far:



var listt = document.getElementById('content1'); 
var selectedvalue = [listt.selectedIndex].text;

More From » list

 Answers
20

You can use the innerText property for most browsers, but the textContent property for Firefox:



<ul>
<li id=myLi>Hello, world</li>
</ul>

var li = document.getElementById(myLi)
console.log(li.textContent || li.innerText);


Here is a fiddle to demonstrate.



If you are using jQuery (you say you are, but I see no evidence) it would be as simple as using the .text() function:



$(#myLi).text();


If your <li> contains HTML markup too, you may want that. In this case you need to use the innerHTML property:



document.getElementById(myLi).innerHTML;


Again, jQuery has it's own equivalent .html() which caters for all sorts of different browsers:



$(#myLi).html();

[#76394] Friday, August 9, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
serena

Total Points: 488
Total Questions: 125
Total Answers: 114

Location: Estonia
Member since Wed, Jun 8, 2022
2 Years ago
;