...

View Full Version : val() method of jquery



johnmerlino
06-30-2010, 07:20 PM
Hey all,

let's say this.filter_id refers to the select element below, which has three options:


<select id="filter_1">
<option value="0">Filter:</option>
<option value="2009-10-01">2009-10-01</option>
<option value="2009-11-20">2009-11-20</option>
</select>

var filter_value = jQuery(this.filter_id).val();
In this case, is the val() method returning the value (0) of the first option element or does it return the value of the collection of option elements as an array?

jQuery help page says this:
Get the current value of the first element in the set of matched elements.

Thanks for any response.

tomws
06-30-2010, 09:04 PM
The "first element in the set of matched elements" is the first select (or any other input) matched by the selectors used. In your case, the set is just one element, so the val() is the value of that element - whatever is selected.

You can examine the operation with a simple example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#filter_1').change(function(){
alert($(this).val());
});
});
</script>
</head>
<body>
<select id="filter_1">
<option value="0">Filter:</option>
<option value="2009-10-01">2009-10-01</option>
<option value="2009-11-20">2009-11-20</option>
</select>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum