I add new divs with jQuery append(), and I want to be able to get the individual content and attributes of the last div and previous ones onclick.
<!DOCTYPE html>
<script type='text/javascript'>
$(document).ready(function(){
$('#ClickDiv').live("click",function(){
IaNm=prompt("Enter Name","Name here...");
IaLang=prompt("Enter Lang","Lang here...")
$('.box:last').append('<div class="box" lang="'+IaLang+'">'+IaNm+'</div>')
})
$('.box').click(function(){
LstNm=$(this).text()
LstT=$(this).attr('lang')
$('b').html(LstNm+' : '+LstT)
})
})
</script>
<input type='button' value='New Div' id='ClickDiv'>
<div class='box' lang='en'>one</div>
<div class='box' lang='fr'>two</div>
<div class='box' lang='de'>three</div>
<b></b>
You need to change this line:
to
If you want to catch the click event for new elements added after the document load event
EDIT:
change
$('.box:last').append( ... )to$('.box:last').after(... )so the new element will be added after the las elment and not inside of it