0

I have this array:

var names = [
    "Name1",
    "Name2",
    "Name3"
];

I converted this to Unorder List for HTML:

for(i = 0; i < names.Length; i++){
    text += "<li>" + names[i] + "</li>";
}
text += "</ul>";


document.getElementById("choices").innerHTML = text;

Now, I got this:

  • Name1
  • Name2
  • Name3

And I am happy with the result. But now I want if someone click on Name2 so it alert me the index of the value. What I want is it should in Mobile App (Cordova) so that when user click on List Item it will show details on other activity (Some other page).

PS: I checked:

var index = $( "li" ).index( this );

and

var index = $("ul li.active").index();

But seems like these are not made for me.

May be I should Dynamically assign ID's to each <li> item? What should I do now?

Community
  • 1
  • 1
fWd82
  • 840
  • 1
  • 13
  • 31
  • 1
    I was making one more mistake as well: `$("#choices").click(function() { // choices is the DOM element that has my all the
  • s. var index = $(this).index(); $("span").text( "Index is: #" + index ); });` | `#choices` was `
    ` I should use `
  • ` instead of `#choices
    `
  • – fWd82 Feb 15 '17 at 06:40