Note #1:
Do not use click
or bind
if your items are generated dynamically.
Note #2:
If you use a old jquery version, you should use live
(version deprecated: 1.7, removed: 1.9).
Well, you can use live
or on
(best option) maybe you can fix it.
Example (best way):
You html(generated dynamic):
<ul id="mylist">
<li><a href="" id="...">...</a></li>
<li><a href="" id="...">...</a></li>
<li><a href="" id="...">...</a></li>
<li><a href="" id="...">...</a></li>
<li><a href="" id="...">...</a></li>
</ul>
You JS:
$('#mylist').on('click', 'a', function(){
alert( $(this).attr('id') );
})
Old way( poor way; deprecated ):
$('#mylist li a').live('click', function(){
alert( $(this).attr('id') );
})
Learn here:
http://api.jquery.com/on/
And here: https://stackoverflow.com/a/11878976/901197
Full example:
JS:
$(document).ready(function(){
var elements = [
{id:1, text: "A", data: 'Hi'},
{id:2, text: "B", data: 'Hello'},
{id:3, text: "C", data: 'Hola'},
{id:4, text: "D", data:'Ciao'},
{id:5, text: "E", data:'Priviet'},
{id:6, text: "F", data: 'Bonjour'},
{id:7, text: "G", data:'Hallo'},
{id:8, text: "H", data:'Olá'}
];
$.each(elements, function( index, element ){
var li = document.createElement('li'),
a = document.createElement('a');
$(a).attr('id', element.id)
$(a).data('hello', element.data)
$(a).text(element.text)
$(li).append(a)
$('#dynamics').append( li )
})
$('#dynamics').on('click', 'a', function(){
var self = $(this);
alert('You clicked on ' + self.text() + '. ' + self.data('hello') + ' id #' + self.attr('id') );
})
});
HTML:
<ul id="dynamics"></ul>
Try here:
http://codepen.io/olaferlandsen/pen/rLEZqw