4

I had someone who did this script (credit Chyno Deluxe) that generate a list of menu whatever we write on the box, the question is, I need to generate a sequence of number that continuously added to it

here is the example needed,

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

the number generated beside item'#' , 1,2,3,4,5,6

I had this that generate number, but it was fixed number, here

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

this one only make like this

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

the 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 with jquery code that will generate

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

I need to improve it by adding tag id='' + number on it, like this

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

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

Hope I can get help with this here, Thank you guys :)

5
  • You need to provide a sample of what you've tried instead of just some samples and asking us to do it for you. Commented Dec 15, 2015 at 17:56
  • I know, I just need to know about basic thing that can help to put a number in it, I just do this for my school project and learning purpose :/ Commented Dec 15, 2015 at 17:59
  • This isn't a do my homework site. If you show us the code you've done, we will be happy to help. You'll learn a lot by trying it for yourself. Commented Dec 15, 2015 at 18:01
  • sorry for my english, I can't explain very clear, it's not just about work, I do great with css and html, and started to love jquery and I really new, this is how I started to get to know more , this is what I had.. Commented Dec 15, 2015 at 18:04
  • You should probably read up on string concatenation in Javascript. You're allowed to say "string"+27 and js will read it as "string27". So just set the id to "item"+numCurrentlyOn, and gee gee. Also, needless jQ, "get them while they're young", etc etc. Commented Dec 15, 2015 at 18:06

2 Answers 2

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

So what you want is just add the id correlated to index of the line the user entered . And this is just i+1 !

So:

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
Sign up to request clarification or add additional context in comments.

4 Comments

thank you Sir, for kind of work.. I really appreciate it, I will try it and see if I can make it work, again tq
Thank you so much!! it work, I was trying a week to get this, this is so silly how I try a lot of line only solve by single line... god bless you!
@okezumi no problem! Next time make a question after 3 days haha. This code you picked is not easy to follow for non experienced users, but with comments it should be easily understood. Except for the html handling, almost all is just js loops and string concatenation ;)
tq so much juvian, I can't believe I can sleep now :D
0

concatenate "item" with the iterator of your for loop plus one (i + 1). and set that as the id while looping through the items. (Note that i starts at zero so if you want it to start with 1 then you have to add 1 to it)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.