How to get Value / Text of a List item Javascript

2024/2/27 9:12:23

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;
Answer

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();
http://en.ppmy.cn/q/41092.html

Related Q&A

Importing web3 to next js results in error

I am using next js and material ui to build a demo dapp for learning. I have metamask installed and so far I can only setup a "connect to wallet" button. I am stuck at a point where I have to…

When using the YUI compressor, should I combine then minify, or minify then combine?

Ive read some people saying there can be issues if you combine and then minify, and that you should always minify then combine (See for example the comments on this question.) However, Ive never seen a…

Traverse 2D Array (Matrix) Diagonally

So I found this thread that was extremely helpful in traversing an array diagonally. Im stuck though on mirroring it. For example:var m = 3; var n = 4; var a = new Array(); var b = 0;for(var i = 0; i &…

Is there a way to overlay a canvas over a fullscreen HTML5 video?

Im using videojs to play video, and Ive done some manipulation on the video content frame by frame and display it in a <canvas> (ID: display).When the video is playing, <canvas> can display…

How to integrate Stripe Connect with Node.js?

So im at the part where you sign in your Stripe account and it redirects you to my website once done. I dont know what to do next and the documentation is difficult to comprehend because i dont have an…

Is there a difference in using a constructor to create an object versus returning an object?

Is there any difference in how these functions operate? The first one is more typically of what I think about when thinking of a constructor.Example 1: using this to name and set properties. Then usi…

rotate svg marker with multiple paths using google maps javascript

When I use the below simple SVG it works well with the rotation method var car = "M17.402,0H5.643C2.526,0,0,3.467,0,6.584v34.804c0,3.116,2.526,5.644,5.643,5.644h11.759c3.116,0,5.644-2.527,5.644-5.…

Javascript click event firing twice, even with stopPropagation

I have a set of items like this:<label for="Cadenza-1" class="cars"><input type="checkbox" value="1" alt="Cadenza" id="Cadenza-1" na…

particles.js not covering entire page

Im trying to use particles.js as background, but I am not able to set the canvas as a full-size background.I tried at least 10 different solution from similar issues but nothing worked. The canvas alwa…

Node Js mysql(and mysql2) ECONNRESET

i am currently trying to connect to a MySQL server on the internet using Node.Js with the mysql or the mysql2 NPM dependencies to use queries and other related stuff.the code is simple...//i import my …