Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    189
    Thanks
    38
    Thanked 3 Times in 3 Posts

    val() method of jquery

    Hey all,

    let's say this.filter_id refers to the select element below, which has three options:
    Code:
    <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.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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:
    Code:
    <!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>
    Are you a Help Vampire?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •