I'm trying to change the alt of the image, I'm clicking by selecting the image's class (add_answer
)
Note: .add_answer
shows up multiple times inside different containing div
's
jQuery(function(){ // Add Answer jQuery(".add_answer").click(function(){ var count = $(this).attr("alt"); count++; $('.a_type_'+count+'').show(); $(this).parents("div:first").$('.add_answer').attr("alt", count); }); });
This line doesn't seem to be working, how do I select this add_answer
class by way of it's parent div
$(this).parents("div:first").$('.add_answer').attr("alt", count);
Anyone else have an idea?
I'm trying having trouble decreasing the alt
value on the .add_answer
image when .destroy_answer
is clicked
jQuery(function(){ // Hide Answer jQuery(".destroy_answer").click(function(){ $(this).parents("div:first").hide(); var count = $('.add_answer').attr("alt"); count--; $('.add_answer',$(this).parent('div:first')).attr('alt',count); }); });
Problem line:
$('.add_answer',$(this).parent('div:first')).attr('alt',count);
jQuery parentsUntil() Method The parentsUntil() method returns all ancestor elements between the selector and stop. An ancestor is a parent, grandparent, great-grandparent, and so on.
The :parent Selector page on jQuery says: Select all elements that have at least one child node (either an element or text). So $('div') would select all divs and $('div:parent') would select only those with children.
jQuery parent() Method The parent() method returns the direct parent element of the selected element. This method only traverse a single level up the DOM tree.
The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.
you can use the parent div as the scope:
$('.add_answer',$(this).parent('div:first')).attr('alt',count);
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