?
Solved

jQuery Each - Value of Label for=""

Posted on 2011-09-17
3
Medium Priority
?
467 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 3

Accepted Solution

by:
FrankoH earned 2000 total points
ID: 36556097
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
ID: 36556412
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
ID: 36557118
Thanks.  Exactly what I was after.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…
Suggested Courses

764 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