Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get computed number of ordered list item

Is there a way to get the content of an ordered list item's number?

var list = document.getElementById('list');
list.style.listStyleType = 'upper-roman';
   
<ol class="list" id="list">
  <li class="list__item">apple</li>
  <li class="list__item">banana</li>
  <li class="list__item" id="target">orange</li>
  <li class="list__item">pear</li>
</ol>

That will produce a list of items like this.

I. apple
II. banana
III. orange
IV. pear

Is there a way to get the III string of text of the #target list item?

EDIT:

Roman characters here are just an example. I'd like the ability to access to the content provided by any of the list-style-type options.

like image 931
1252748 Avatar asked Nov 21 '16 17:11

1252748


People also ask

What is ordered list in computer?

An ordered list typically is a numbered list of items. HTML 3.0 gives you the ability to control the sequence number - to continue where the previous list left off, or to start at a particular number.

Which attribute can be used to change its number within an ordered list?

The value attribute. The value attribute changes the number of a specific list item and those that follow it. Since the ordered list is the only type with sequentially numbered items, the value attribute is valid only when used within an <li> tag inside an ordered list.

How do you start an ordered list from a number in HTML?

The start attribute specifies the start value of the first list item in an ordered list. This value is always an integer, even when the numbering type is letters or romans. E.g., to start counting list items from the letter "c" or the roman number "iii", use start="3".


2 Answers

The only way I can think of doing this is the following:

1) Get the index of the item (e.g. 3)

2) Have a function like this: https://stackoverflow.com/a/9083076/1324321

3) Run the index through the function

like image 199
mikestreety Avatar answered Oct 13 '22 02:10

mikestreety


I created a jsfiddle here which can display the chosen selection to the user. Although javascript is not holding this as a string, I first find the index of the selected list item, then I recreate a list of that one item with the "start" attribute being set to that index.

Here is the HTML:

<ol>

  <li>first</li>
  <li>second</li>
  <li id="active">third</li>
  <li>Fourth</li>
</ol>

<br/>
<br/>
<div id='selected'>
</div>

And the JQuery:

$(document).ready(function() {
  var intt = $('li').index($('#active')) + 1;
  $('#selected').html('<ol start="' + intt + '"><li></li></ol>');
});

JSFIDDLE

like image 23
Jacob Morris Avatar answered Oct 13 '22 01:10

Jacob Morris