Coerce all array values to strings before comparison in val(). Fixes bug #7123.
[jquery.git] / src / attributes.js
index a76695b..147c353 100644 (file)
@@ -136,7 +136,7 @@ jQuery.fn.extend({
        },
 
        val: function( value ) {
-               if ( value === undefined ) {
+               if ( !arguments.length ) {
                        var elem = this[0];
 
                        if ( elem ) {
@@ -164,8 +164,9 @@ jQuery.fn.extend({
                                                var option = options[ i ];
 
                                                // Don't return options that are disabled or in a disabled optgroup
-                                               if ( option.selected && !option.disabled && 
+                                               if ( option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) && 
                                                                (!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) {
+
                                                        // Get the specific value for the option
                                                        value = jQuery(option).val();
 
@@ -209,10 +210,15 @@ jQuery.fn.extend({
                                val = value.call(this, i, self.val());
                        }
 
-                       // Typecast each time if the value is a Function and the appended
-                       // value is therefore different each time.
-                       if ( typeof val === "number" ) {
+                       // Treat null/undefined as ""; convert numbers to string
+                       if ( val == null ) {
+                               val = "";
+                       } else if ( typeof val === "number" ) {
                                val += "";
+                       } else if ( jQuery.isArray(val) ) {
+                               val = jQuery.map(val, function (value) {
+                                       return value == null ? "" : value + "";
+                               });
                        }
 
                        if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
@@ -293,7 +299,14 @@ jQuery.extend({
                                                jQuery.error( "type property can't be changed" );
                                        }
 
-                                       elem[ name ] = value;
+                                       if ( value === null ) {
+                                               if ( elem.nodeType === 1 ) {
+                                                       elem.removeAttribute( name );
+                                               }
+
+                                       } else {
+                                               elem[ name ] = value;
+                                       }
                                }
 
                                // browsers index elements by id/name on forms, give priority to attributes.
@@ -331,7 +344,7 @@ jQuery.extend({
 
                        // Ensure that missing attributes return undefined
                        // Blackberry 4.7 returns "" from getAttribute #6938
-                       if ( !elem.attributes[ name ] && !elem.hasAttribute( name ) ) {
+                       if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
                                return undefined;
                        }