Reputation: 1554
For each instance of a class (selected
) I need to get the value of the previous class (imageID
). The plan is to get the imageID
value, trigger an ajax call and set the attribute of each instance of the class (selected
) depending on the ajax response.
The code I have so far is
$(document).ready(function() {
$('.selected').each(function(){
var image_id = $('.selected').prev('.imageID').val();
alert(image_id);
});
});
However, all this does is trigger an alert for each instance of selected
but always with the value of the first instance.
Upvotes: 1
Views: 121
Reputation: 144659
$('.selected') // selects all the .selected elements
.prev('.imageID') // all very previous siblings that have `ImageId` class
.val(); // as getter returns value of the first element in the set
Use the this
keyword:
var image_id = $(this).prev('.imageID').val();
Upvotes: 1