Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to refer to a <li> index?

Is it possible to refer to a <li> index? I want to do that to be able to add/remove/reorder the <li> tags without having to manually change the reference number.

Here is an example with some made-up HTML that outlines how I think doing that could work:

<ol>
     <li>...</li>
     <li ref="some-ref">Some text that I want to refer to later ...</li>
     <li>...</li>
     <li>According to <liref ref="some-ref" />, ...</li>
</ol>

The desired output would of course be:

  1. ...
  2. Some text that I want to refer to later ...
  3. ...
  4. According to 2., ...

Is there something like this in real-life HTML?

like image 776
zbr Avatar asked Aug 19 '14 11:08

zbr


People also ask

How do I read or interpret an index?

How do I read or interpret an index? An index is a tool that simplifies the measurement of movements in a numerical series. Most of the specific CPI indexes have a 1982-84 reference base. That is, BLS sets the average index level (representing the average price level)-for the 36-month period covering the years 1982, 1983, and 1984-equal to 100.

What is the difference between an index and an index?

"Indices" is used when referring to mathematical, scientific and statistical contexts. It is used to refer to a numbers, symbols, and figures comparing a value to a standard. "Indexes" is usually used in reference to written documents, such as bibliographical or citation listings.

What are indexes used for in writing?

An index is a list of all the names, subjects and ideas in a piece of written work, designed to help readers quickly find where they are discussed in the text. Usually found at the end of the text, an index doesn’t just list the content (that’s what a table of contents is for), it analyses it. Where are indexes used?

What is an index in a report?

An index provides a map to a report’s content. It does this through identifying key themes and ideas, grouping similar concepts, cross-referencing information and using clear formatting. A good index will: cross-reference information to point to other headings of interest or preferred terms. For example, a back-of-the-book index might read:


2 Answers

HTML doesn't provide such mechanism. You need server-side or client-side scripting to achieve this.

You can also try using CSS counter properties: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Counters

like image 97
atlavis Avatar answered Nov 01 '22 05:11

atlavis


For what it's worth, I have accomplished what I wanted using jQuery. It doesn't answer the question directly, but can be useful to someone. It works for nested lists too.

Take this example:

<ol>
    <li>...</li>
    <li>Nested list
        <ol>
            <li>...</li>
            <li id="ref1">Some text that I want to refer to later ...</li>
            <li>...</li>
            <li id="ref2">Different text that I want to refer to ...</li>
            <li>According to <span class="ref" name="#ref2"></span>, ...</li>
        </ol>
    </li>
    <li>According to <span class="ref" name="#ref1"></span>, ...</li>
</ol>

Using this jQuery code:

$(function() {
    $('.ref').each(function() {
        var refLi = $($(this).attr('name'));
        var refLiAncestorLis = refLi.parents('li');
        var reference = '';
        for (var i = refLiAncestorLis.length - 1; i >= 0; i--) {
            reference += $(refLiAncestorLis[i]).index() + 1 + '.';
        }
        reference += refLi.index() + 1 + '.';
        $(this).text(reference);
    });
});

You can get this output:

  1. ...
  2. Nested list
    1. ...
    2. Some text that I want to refer to later ...
    3. ...
    4. Another text that I want to refer to ...
    5. According to 2.4., ...
  3. According to 2.2., ...
like image 37
zbr Avatar answered Nov 01 '22 06:11

zbr