LeBlaireau LeBlaireau -4 years ago 105
jQuery Question

Apending variable in Jquery selector

I have this selector

self = $('.trim .person:nth-child('+index+')');
self.append('<img src="'+element.profilePic+'" class="profilePic" />');

I then want to use this select a class within

$(self + '.spacer').css('opacity', 0.5);

I also tried

self + $('.spacer').css('opacity', 0.5);

Answer Source

Set it as the context parameter in jQuery.

$('.spacer', self).css('opacity', 0.5);

Or use find() method to get element within the current jQuery object.

self.find('.spacer').css('opacity', 0.5);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download