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?

3
  • 1
    You really should make it as '<li onclick="doStuff(' + i + )"'>... as this way you don't need to do anything Commented Feb 15, 2017 at 6:36
  • 1
    I was making one more mistake as well: $("#choices").click(function() { // choices is the DOM element that has my all the <li>s. var index = $(this).index(); $("span").text( "Index is: #" + index ); }); | #choices was <div> I should use <li> instead of #choices <div> Commented Feb 15, 2017 at 6:40
  • If I am losing lot of reputation because of this post I will delete it. I think the problem is well discussed bellow so you should not downvote it. May be other users got it helpful. Thank You. Commented Feb 15, 2017 at 7:03

7 Answers 7

1

You can directly get index using index method.

$("li").click(function () {
  alert($(this).index());
});

If you have multiple ul elements on page and you want to bind click event to some specific element you can do this.

To bind click on element by id

$("#YourUlId li").click(function () {
      alert($(this).index());
});

To bind click event by class

$(".YourUlClass li").click(function () {
      alert($(this).index());
});
Sign up to request clarification or add additional context in comments.

3 Comments

I just have one question: What if I have multiple <ul> in same context or file.?
You can bind click function to specific ul li by id or class
Like $("#YourUlId li").click
1

$( "li" ).index will return elements index with respect to all li elements in DOM.

You need to use .index() with jquery object of clicked element. It will return the elements index in its parent container:

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

Comments

1

Pass the id dynamically in for loop

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

assign the click event, u will get the id by below code

$(li).click(function(){
   alert($(this).attr("id"));
});

1 Comment

Genius, +1 for text += "<li id="+i+">" + names[i] + "</li>"; Thank You.
1

use this for getting the current element

<ul>
<li>Male</li>
<li>Female</li>
</ul>

 <script>
$("li").click(function(){
 alert($(this).index())
})
 </script>

1 Comment

Very easy to understand. Thank You. :)
1

See below working snippet

var names = [
    "Name1",
    "Name2",
    "Name3"
];
var text='<ul>';
for(i = 0; i < names.length; i++){
    text += "<li>" + names[i] + "</li>";
}
text += "</ul>";
document.getElementById("choices").innerHTML = text;

$('li').on('click',function(){
  alert($(this).index())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="choices"></div>

1 Comment

Thank You. +1 for Complete Code Snippet.
1

Using native javascript, you can just create a function that will alert the index of the array value corresponding the li element value/id

First you attach the function on the choices

document.getElementById('Name2').setAttribute('onclick','checkIndex(this)');

then loop through the array and hunt for the matching array value

function checkIndex(item){
  for(i=0;i<names.length;i++){
    if(names[i] == item.innerHTML){ //or item.id
      alert(names.indexOf(names[i]));
    }
  }
}

2 Comments

Thank You for the answer. I pick an idea from it. Though I do it with Array and not with DOM <li>s.
Glad you got an idea
0

I would handle it in a declarative way, using html data-attributes, handled by jquery. see here

Here's an example.

var names = ["jack", "mary", "lou", "andrew"];
var text = "<ul>";
for (var i = 0; i < names.length; i++) { 
  text += "<li data-id='" + i + "'>" + names[i] + "</li>";
}
text += "</ul>";
document.getElementById("choices").innerHTML = text;

$("li").click(function(e){
   alert($(this).data("id"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<body>
  My Choice
  <div id="choices"></div>
</body>

</html>

Of course you can change data-id with whatever you want, or either add other attributes (data-page ? data-txt ? ) and handle all this in a proper way.

Also, dynamically assign ids to your list is a possibility, but I prefer this because I find it more flexible.

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.