[jQuery] Click scope issue
Hi,
I am trying to resolve a scoping issue I have with the click event
handler in jQuery, here is my code:
<script type="text/javascript">
for(i in $('.container').get()) {
$(".container:eq("+i+") .button").click(function(){
alert(i);
});
}
</script>
<div class="container">
<div class="button">Click (should return 0)</div>
</div>
<div class="container">
<div class="button">Click (should return 1)</div>
</div>
<div class="container">
<div class="button">Click (should return 2)</div>
</div>
The jQuery above will loop through. However, each div will alert "2"
instead of its corresponding count.
Can anyone help? I'm very new to jQuery and thought I understood it
well until now.
Cheers,
Luke