Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery select class inside parent div

Tags:

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); 
like image 747
adam Avatar asked Dec 26 '08 23:12

adam


People also ask

Where can I find grandparents in jQuery?

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.

What does $( Div parent will select?

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.

Which jQuery method returns the direct parent element of the selected element?

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.

How to use parent() in jQuery?

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.


1 Answers

you can use the parent div as the scope:

 $('.add_answer',$(this).parent('div:first')).attr('alt',count); 
like image 159
Eran Galperin Avatar answered Sep 28 '22 12:09

Eran Galperin