I'm trying to get the class name from an selection of images when clicked.
The code below retrieves the first images class name but when I click on my other images with different class values they display the first images class.
How can I get each images class value?
HTML
<a href="#" title="{title}" class="bg"><img src="{image:url:small}" alt="{title}" class="{image:url:large}" /></a>
jQuery Code
$(".bg").click(function(){
var1 = $('.bg img').attr('class');
Try this instead:
$(".bg").click(function(){
var1 = $(this).children('img').attr('class');
Try:
$(".bg").click(function(){
var1 = $(this).attr('class');
});
The above might, on reflection, not be quite what you're after. I'd suggest trying:
$('.bg img').click( // attaches the 'click' to the image
function(){
var1 = $(this).attr('class');
});
Or:
$(".bg").click(function(){
var1 = $(this).find('img').attr('class'); // finds the 'img' inside the 'a'
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With