5 ], function( jQuery, support ) {
8 rspaces = /[\x20\t\r\n\f]+/g;
11 val: function( value ) {
12 var hooks, ret, isFunction,
15 if ( !arguments.length ) {
17 hooks = jQuery.valHooks[ elem.type ] ||
18 jQuery.valHooks[ elem.nodeName.toLowerCase() ];
22 ( ret = hooks.get( elem, "value" ) ) !== undefined
29 return typeof ret === "string" ?
31 // Handle most common string cases
32 ret.replace( rreturn, "" ) :
34 // Handle cases where value is null/undef or number
35 ret == null ? "" : ret;
41 isFunction = jQuery.isFunction( value );
43 return this.each( function( i ) {
46 if ( this.nodeType !== 1 ) {
51 val = value.call( this, i, jQuery( this ).val() );
56 // Treat null/undefined as ""; convert numbers to string
60 } else if ( typeof val === "number" ) {
63 } else if ( jQuery.isArray( val ) ) {
64 val = jQuery.map( val, function( value ) {
65 return value == null ? "" : value + "";
69 hooks = jQuery.valHooks[ this.type ] || jQuery.valHooks[ this.nodeName.toLowerCase() ];
71 // If set returns undefined, fall back to normal setting
72 if ( !hooks || !( "set" in hooks ) || hooks.set( this, val, "value" ) === undefined ) {
82 get: function( elem ) {
84 var val = jQuery.find.attr( elem, "value" );
89 // option.text throws exceptions (#14686, #14858)
90 // Strip and collapse whitespace
91 // https://html.spec.whatwg.org/#strip-and-collapse-whitespace
92 jQuery.trim( jQuery.text( elem ) ).replace( rspaces, " " );
96 get: function( elem ) {
98 options = elem.options,
99 index = elem.selectedIndex,
100 one = elem.type === "select-one" || index < 0,
101 values = one ? null : [],
102 max = one ? index + 1 : options.length,
107 // Loop through all the selected options
108 for ( ; i < max; i++ ) {
109 option = options[ i ];
111 // IE8-9 doesn't update selected after form reset (#2551)
112 if ( ( option.selected || i === index ) &&
114 // Don't return options that are disabled or in a disabled optgroup
115 ( support.optDisabled ?
116 !option.disabled : option.getAttribute( "disabled" ) === null ) &&
117 ( !option.parentNode.disabled ||
118 !jQuery.nodeName( option.parentNode, "optgroup" ) ) ) {
120 // Get the specific value for the option
121 value = jQuery( option ).val();
123 // We don't need an array for one selects
128 // Multi-Selects return an array
129 values.push( value );
136 set: function( elem, value ) {
137 var optionSet, option,
138 options = elem.options,
139 values = jQuery.makeArray( value ),
143 option = options[ i ];
144 if ( option.selected =
145 jQuery.inArray( jQuery.valHooks.option.get( option ), values ) > -1
151 // Force browsers to behave consistently when non-matching value is set
153 elem.selectedIndex = -1;
161 // Radios and checkboxes getter/setter
162 jQuery.each( [ "radio", "checkbox" ], function() {
163 jQuery.valHooks[ this ] = {
164 set: function( elem, value ) {
165 if ( jQuery.isArray( value ) ) {
166 return ( elem.checked = jQuery.inArray( jQuery( elem ).val(), value ) > -1 );
170 if ( !support.checkOn ) {
171 jQuery.valHooks[ this ].get = function( elem ) {
172 return elem.getAttribute( "value" ) === null ? "on" : elem.value;