[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

jQuery Each - Value of Label for=""

Posted on 2011-09-17
3
Medium Priority
?
473 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 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

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

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 will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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

872 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