Mahadewa schrieb:
> Hi all,
>
> I have a question about .each construct. I have the following snippet code,
> but for some reason it doesn't do what I expected it to do. Have I missed
> something ?
>
> $(".MyButton").each(function(){
>
> this.click(function(){ alert(this.id;) });
>
> });
>
> Thanks for the help,
> Chris
With each() you iterate over every element inside the array-like jQuery
object (all matched elements from your selection). Thus "this" inside
the function you pass to each() refers to a DOM element.
If you need to apply jQuery methods, you will have to wrap "this" aka
the element in a jQuery object again:
$(".MyButton").each(function(){
$(this).click(function(){ alert(this.id;) });
});
Your example would be written like this of course:
$(".MyButton").click(function() { alert(this.id;) });
-- Klaus
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/