Solved

jQuery Each - Value of Label for=""

Posted on 2011-09-17
3
465 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 500 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

734 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