Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse the numbering order for ordered lists

Tags:

I would like to have the numbering of the ordered list in descending order. live example here:

demo

But instead of having the counte span multiple ol, I would like the counter to reset after each ol.

So, the desired result for the live demo would be:

[3] 1
[2] 2
[1] 3

[2] 4
[1] 5

Does anyone have an idea how to modify the code from the live demo (or know a better solution that is supported by most browsers) to have the above behavior?

Also, is there a way to make the [ ] "copyable" with copy-paste to Word docs or any open source alternative? Because for the above example it interprets only the numbers without the square parenthesis.

like image 909
AMayer Avatar asked Jan 07 '18 07:01

AMayer


People also ask

How can we reverse the order of items in ordered lists?

To set reversed ordering of list items in an ordered list, we can use the reversed attribute of the <ol> element in HTML. It was introduced in HTML5 and shows the numbering in descending order. Example 1: HTML.

How do you reverse a numbered list?

How to Reverse a Numbered List. To reverse a numbered list in Word, select the list in your Word document and press Ctrl+C to copy it. Place the cursor in the Word file where you want to put your reordered list, click the down arrow on the “Paste” button, and select “Paste Special” from the drop-down menu.

Which attribute of the ordered list shows the numbers in reversed order?

The reversed attribute is a boolean attribute. When present, it specifies that the list order should be descending (9,8,7...), instead of ascending (1, 2, 3...).

How do you reverse order a list in Python?

Python lists can be reversed in-place with the list. reverse() method. This is a great option to reverse the order of a list (or any mutable sequence) in Python. It modifies the original container in-place which means no additional memory is required.


1 Answers

You have to set the counter-reset value for each ol is equal to the number of child + 1

var ol_elements = document.getElementsByTagName("ol");
//console.log(ol_elements[0].children.length)
for (var i = 0; i < ol_elements.length; i++) {
  ol_elements[i].setAttribute('style', 'counter-reset: item ' + (ol_elements[i].children.length + 1));
}
body {
  font: 13px Verdana
}

ol {
  list-style-type: none;
  margin-left: 20px;
  padding: 0px;
}

ol>li {
  counter-increment: item -1;
}

ol>li:before {
  content: "[" counter(item) "]";
  padding-right: 10px;
}
<div id="content">
  <ol>
    <li>1</li>
    <li>2</li>
    <li>3</li>
  </ol>
  <ol>
    <li>4</li>
    <li>5</li>
  </ol>
  <ol>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
  </ol>
</div>
like image 122
Bhuwan Avatar answered Sep 21 '22 13:09

Bhuwan