Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do sub selects on already selected element?

Markup:

<div class="foo">     <img src="loading.gif" class="loading" style="display: none;" /> </div> 

Js:

$("div[class='foo']").click(function(e) {     e.preventDefault();     $(this).hide();     $(/* somehow select the loading img of exactly this div with class foo (not others) */).show(); }); 
like image 294
randomguy Avatar asked Aug 11 '10 13:08

randomguy


People also ask

How do I select elements when I already have a DOM element?

If you have a variable containing a DOM element, and want to select elements related to that DOM element, simply wrap it in a jQuery object. var myDomElement = document. getElementById( "foo" ); // A plain DOM element.

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.

How do you select an element with a particular class selected?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.

How do you select a particular option in a select element in jQuery?

Syntax of jQuery Select Option$("selector option: selected"); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $("selector option: selected").


2 Answers

$("div[class='foo']").click(function(e) {     e.preventDefault();     $(this).hide();     $('img.loading', this).show(); }); 
like image 134
simplyharsh Avatar answered Sep 20 '22 23:09

simplyharsh


If you want any descendant of the given element you can use find():

$(this).find(".foo"); 

If you know you only want to search for the first-level children elements, you can use children():

$(this).children(".foo"); 
like image 33
matt b Avatar answered Sep 22 '22 23:09

matt b