How to generate sequence number with jquery?

I had someone who made this script (credit Chyno Deluxe) that generates a list of menus that we write on the box, the question is, do I need to create a sequence of numbers that is constantly added to it

here is an example needed

<li id='item1'></li> <li id='item2'></li> <li id='item3'></li> <li id='item4'></li>

the number generated next to the '#' element, 1,2,3,4,5,6

I had this to generate a number, but it was a fixed number, here

$.map($(Array(8)),function(val, i) { return i; })

this one only does it

1,2,3,4,5,6,7,8

script

(function($) {
        "use strict";
        var t2h = {
                buildHTML: function() {
                        var i, list, type = this.htmlSelect.options;
                        if (type[1].selected) {
                                //console.log(type[1].text);
                                list = "<select>";
                                list += "\n";
                                for (i = 0; i < this.get_items().length; i++) {
                                        list += "  <option>";
                                        list += this.get_items()[i];
                                        list += "</option>";
                                        list += "\n";
                                }
                                list += "</select>";

you can see the demo below using jquery code which will generate

<select>
<option>menu 1</option>
<option>menu 2</option>
</select>

I need to improve it by adding id = '' + number tag on it, for example

<select>
<option id='item1'>menu 1</option>
<option id='item2'>menu 2</option>
</select>

demo: [link] http://codepen.io/diden/pen/YwwVKO

Hope I can get help with this, thanks guys :)

+4
2
for (i = 0; i < this.get_items().length; i++) { // here i will go from 0 to the length of the items the select will have -1 
    list += "  <option>"; // for each of these values of i, we add the first part of the html to list, which is a string variable
    list += this.get_items()[i]; // this adds the ith text that the user wrote, but it´s 0 index based instead of starting with 1

, , , . + 1!

:

list += "  <option id='item"+(i+1)+"'>"; // we open open and close "" to add (i+1) which is a varibale, and its the id we wanted
+2

"" for (i + 1). id . ( , , , , 1, 1 )

0

Source: https://habr.com/ru/post/1620175/


All Articles