It's a small piece of code, but I can't get it to work.
I have a menu which is filled dynamically with javascript, and I add an eventlistener on click for every button.
Now every button needs a different function, so I loop through them

```
var list = $("a");
for (var i=0; i<list.length; i++) {
    $(list[i]).on("click",function(){alert(i);});
}

```

With this code every button will give me an alert with 5, instead of 1,2,3,...
How do I fix this?
['https://Stackoverflow.com/questions/22361630', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']
Use [each()](https://api.jquery.com/each/) that's what it's for:

```
var list = $("a");
list.each(function(index){
    $(this).on("click", function(){alert(index);});
});

```

[DEMO](http://jsfiddle.net/7HeeA/)
in your html code call a function favbrowser whenever a item is selected using onchange event by writting 

```
<select id="myList" onchange="favBrowser()">
  <option></option>
</select>

```

and then in javascript do this to print the name of menu item

```
<script>
function favBrowser()
{
var mylist=document.getElementById("myList");
alert(mylist.options[mylist.selectedIndex].text);
}
</script>

```