Solved

jQuery Each - Value of Label for=""

Posted on 2011-09-17
3
456 Views
Last Modified: 2012-05-12
I'm trying to retrieve the value of the for="" using jQuery's each.  I'm looking for the EventStartMonth and EventEndMonth values.  Am I on the right track or is there a better approach?  Thanks.
<script>
$(function(){
	$.each( $( '.input.datetime label[for$=Month]' ) , function( k , v ){
		alert( v.value );
	});
});
</script>
<div class="input datetime">
 <label for="EventStartMonth">Start</label>
	<select name="data[Event][start][month]" id="EventStartMonth"> 
	<option value="01">January</option> 
	<option value="02">February</option> 
	<option value="03">March</option> 
	</select>
 <label for="EventEndMonth">End</label>
	<select name="data[Event][end][month]" id="EventEndMonth"> 
	<option value="01">January</option> 
	<option value="02">February</option> 
	<option value="03">March</option> 
	</select>
</div>

Open in new window

0
Comment
Question by:radarhill
3 Comments
 
LVL 3

Accepted Solution

by:
FrankoH earned 500 total points
Comment Utility
Hi,

You can simply use $(this) to access the object inside the loop. So you would do something like this:

$.each( $( '.input.datetime label[for$=Month]' ) , function( k , v ){
		alert( $(this).attr("for") );
	});

Open in new window


Thanks
0
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
You have got the label elements all right, but the label doesn't have the value so you have to find the select element inside the label:


$.each($('.input.datetime label[for$=Month] select') , function( k , v ){
  alert($(v).val());
});

or:

$.each($('.input.datetime label[for$=Month]') , function( k , v ){
  alert($(v).find('select').val());
});

Open in new window

0
 
LVL 1

Author Closing Comment

by:radarhill
Comment Utility
Thanks.  Exactly what I was after.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now