Reputation: 845
I want to apply same plugin to two elements in appended div
<div class="main">
<div class="profile"><a href="#">John</a></div>
<div class="msg">aaaaaa</div>
<div class="comment">
<div class="profilec"><a href="#">Jane</a></div>
</div>
</div>
I have pre-pended the above section on click of a button
Now how can i do this
$("#main").prepend(html);
var first= $("#main .main:first").find('.profile').find('a');
var second=$("#main .main:first").find('.comment').find('.profilec').find('a');
$(first,second).myPlugin();
I tried this but it isnt working
Upvotes: 0
Views: 32
Reputation: 144689
You are using $(selector, context)
which simply fails here, you can pass an array of elements to jQuery:
$( [first, second] ).myPlugin();
Or use .add()
method:
first.add(second).myPlugin();
And I would do:
$('#main .main:first a').bar();
Or maybe:
$(html).appendTo('#main').find('a').foo();
Upvotes: 1
Reputation: 67207
A modified form of your code, Please see here for the valid selectors
$("#main").prepend(html);
var first= $("#main .main:first").find('.profile').find('a');
var second=$("#main .main:first").find('.comment').find('.profilec').find('a');
first.myPlugin();
second.myPlugin();
OR
$(".main .profile a").add($(".main .profile a")).myPlugin();
Upvotes: 1
Reputation: 14983
Wouldn't using just the selectors work?
$(".main .profile a, .main .comment .profilec a").myPlugin()
You might not need the find()
at all...
Or, if this is all markup inside .main
, just using
$(".main a").myPlugin()
would do the trick
Upvotes: 1