I would like to use Javascript to extract innerText from the last entry of an html list on a web page. The list uses tabindex, so that seems like the best way of identifying the last entry. However, the list is also dynamic, so the tabindex number of the final entry will change day to day. I'm pretty new at this, but it would seem that I need the script to find the tabindex number of the last entry, and then use document.querySelector to get the innerText of that entry? Here's an example of what the list looks like:
<div id="container">
<ul id=“list”>
<li class=“entry” tabindex=“0” id=“entry0”></li>
<li class=“entry” tabindex=“1” id=“entry1”></li>
<li class=“entry” tabindex=“2” id=“entry2”></li>
<li class=“entry” tabindex=“3” id=“entry3”></li>
<li class=“entry” tabindex=“4” id=“entry4”></li>
</ul>
</div>
I've also been reviewing some similar questions with great answers, but I've had difficulty modifying the code to do exactly what I'm looking for. This is the closest solution I've found on the site(answer provided by Elliot Frisch).
function moveNext(tabInput) {
var lastTab = 3;
var curIndex = tabInput.tabIndex;
var tabs = document.getElementsByTagName("input");
if(curIndex >= lastTab) { //if we are on the last tab then go back to the beginning
curIndex = 0;
}
for(var i=0; i < tabs.length; i++) { // loop over the tabs.
if(tabs[i].tabIndex == (curIndex+1)) { // is this our tab?
tabbables[i].focus(); // Focus and leave.
break;
}
}
Does anybody have any ideas? I'd love to crack this nut.
Thanks for any assistance.
question from:
https://stackoverflow.com/questions/66055187/using-javascript-to-find-the-tabindex-of-the-last-entry-of-a-list-and-then-extra 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…