?
Solved

need to modify class of child image

Posted on 2011-05-12
6
Medium Priority
?
182 Views
Last Modified: 2012-05-11
My code attached successfully removes two classes whenever they appear on a .dayblockhl element, and adds a class dayblockhl to the element that was clicked.

The 4th thing I'm trying to do is add a class to the image with a class of gr_mwc_monthblock that is a child element of the clicked DIV. I need to select the child IMG within the clicked DIV. The syntax of my code must be off. How can I do this?

Thank you!
$(function(){
	$('.dayblock').click(function(){
		$('.dayblockhl').removeClass('dayblockhl');
		$('.gr_mwc_monthblockarrowhl').removeClass('gr_mwc_monthblockarrowhl');
		$(this).addClass('dayblockhl');
		$(this).addClass('gr_mwc_monthblockarrowhl');
	});
});

<div class="dayblock" id="day_5_8_2011">
	<img class="gr_mwc_monthblockarrow" src="art/gr_mwc_monthblockarrow.png" width="9" height="11" border="0" /> May 8<br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock1.png" width="98" height="15" border="0" /><br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock2.png" width="98" height="15" border="0" /><br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock3.png" width="98" height="15" border="0" /><br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock4.png" width="98" height="15" border="0" />
</div>

Open in new window

0
Comment
Question by:bbdesign
  • 4
  • 2
6 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 35749529
You may use : $("gr_mwc_monthblock", this) to select it



$(function(){
	$('.dayblock').click(function(){
		$('.dayblockhl').removeClass('dayblockhl');
		$('.gr_mwc_monthblockarrowhl').removeClass('gr_mwc_monthblockarrowhl');
		$(this).addClass('dayblockhl');
		$(this).addClass('gr_mwc_monthblockarrowhl');
                $("gr_mwc_monthblock", this).addClass("someclass");
	});
});

<div class="dayblock" id="day_5_8_2011">
	<img class="gr_mwc_monthblockarrow" src="art/gr_mwc_monthblockarrow.png" width="9" height="11" border="0" /> May 8<br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock1.png" width="98" height="15" border="0" /><br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock2.png" width="98" height="15" border="0" /><br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock3.png" width="98" height="15" border="0" /><br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock4.png" width="98" height="15" border="0" />
</div>

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 35749537
Correction (class selector : . ) :  $(".gr_mwc_monthblock", this)

$(function(){
	$('.dayblock').click(function(){
		$('.dayblockhl').removeClass('dayblockhl');
		$('.gr_mwc_monthblockarrowhl').removeClass('gr_mwc_monthblockarrowhl');
		$(this).addClass('dayblockhl');
		$(this).addClass('gr_mwc_monthblockarrowhl');
                $(".gr_mwc_monthblock", this).addClass("someclass");
	});
});

<div class="dayblock" id="day_5_8_2011">
	<img class="gr_mwc_monthblockarrow" src="art/gr_mwc_monthblockarrow.png" width="9" height="11" border="0" /> May 8<br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock1.png" width="98" height="15" border="0" /><br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock2.png" width="98" height="15" border="0" /><br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock3.png" width="98" height="15" border="0" /><br />
	<img class="gr_mwc_monthblock" src="art/gr_mwc_monthblock4.png" width="98" height="15" border="0" />
</div>

Open in new window

0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 35749545
Or (direct children) : $(this).children(".gr_mwc_monthblock")
Or again : $(this).find(".gr_mwc_monthblock")
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bbdesign
ID: 35749555
Fantastic! So, is it correct that this is what you are saying:

select anything with a class of gr_mwc_monthblockarrow within "this"?

That seems counterintuitive to me, because I thought you always start with the parent and drill down to the child.
0
 

Author Comment

by:bbdesign
ID: 35749562
I like this best:

$(this).children('.gr_mwc_monthblockarrow').addClass('gr_mwc_monthblockarrowhl');

But thanks again!
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35749574
You're welcome! Thanks for the points!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question