user1044169
user1044169

Reputation: 2866

Refer to attributes from multiple selector in jQuery

I have a bunch of DIVs identified by class screen-cat-comment and I want for each div to execute a plugin that receives as a parameter an attribute value from each DIV.

<div class="screen-cat-comment" catId="1"></div>
<div class="screen-cat-comment" catId="2"></div>
<div class="screen-cat-comment" catId="3"></div>

then, the selector

$('.screen-cat-comment').fragment({ code: $(this).attr('catId') });

Passing catId to myPlugin doesn't work -- from the current code, $(this).attr('catId') returns undefined. Is there any way to re-write this selector to pass attr('catId') to the plugin?

Upvotes: 0

Views: 34

Answers (1)

gdoron
gdoron

Reputation: 150263

this in that context is probably the window...

$('.screen-cat-comment').each(function(){
    $(this).fragment({ code: $(this).attr('catId') });
});

Upvotes: 1

Related Questions