Fixes regression in .attr(), patch by David Flanagan. Fixes #4884.
[jquery.git] / src / attributes.js
1 jQuery.fn.extend({
2         attr: function( name, value ) {
3                 var elem, options, isFunction = jQuery.isFunction(value);
4
5                 if ( typeof name === "string" ) {     // A single attribute
6                         if ( value === undefined ) {        // Query it on first element
7                                 return this.length ?
8                                         jQuery.attr( this[0], name ) :
9                                         null;
10                         } else {                            // Set it on all elements
11                                 for ( var i = 0, l = this.length; i < l; i++ ) {
12                                         elem = this[i];
13                                         if ( isFunction )
14                                                 value = value.call(elem,i);
15                                         jQuery.attr( elem, name, value );
16                                 }
17                         }
18                 } else {                              // Multiple attributes to set on all
19                         options = name;
20                         for ( var i = 0, l = this.length; i < l; i++ ) {
21                                 elem = this[i];
22                                 for ( name in options ) {
23                                         value = options[name];
24                                         if ( jQuery.isFunction(value) )
25                                                 value = value.call(elem,i);
26                                         jQuery.attr( elem, name, value );
27                                 }
28                         }
29                 }
30
31                 return this;
32         },
33
34         hasClass: function( selector ) {
35                 return !!selector && this.is( "." + selector );
36         },
37
38         val: function( value ) {
39                 if ( value === undefined ) {
40                         var elem = this[0];
41
42                         if ( elem ) {
43                                 if( jQuery.nodeName( elem, 'option' ) )
44                                         return (elem.attributes.value || {}).specified ? elem.value : elem.text;
45
46                                 // We need to handle select boxes special
47                                 if ( jQuery.nodeName( elem, "select" ) ) {
48                                         var index = elem.selectedIndex,
49                                                 values = [],
50                                                 options = elem.options,
51                                                 one = elem.type == "select-one";
52
53                                         // Nothing was selected
54                                         if ( index < 0 )
55                                                 return null;
56
57                                         // Loop through all the selected options
58                                         for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
59                                                 var option = options[ i ];
60
61                                                 if ( option.selected ) {
62                                                         // Get the specifc value for the option
63                                                         value = jQuery(option).val();
64
65                                                         // We don't need an array for one selects
66                                                         if ( one )
67                                                                 return value;
68
69                                                         // Multi-Selects return an array
70                                                         values.push( value );
71                                                 }
72                                         }
73
74                                         return values;
75                                 }
76
77                                 // Everything else, we just grab the value
78                                 return (elem.value || "").replace(/\r/g, "");
79
80                         }
81
82                         return undefined;
83                 }
84
85                 if ( typeof value === "number" )
86                         value += '';
87
88                 return this.each(function(){
89                         if ( this.nodeType != 1 )
90                                 return;
91
92                         if ( jQuery.isArray(value) && /radio|checkbox/.test( this.type ) )
93                                 this.checked = (jQuery.inArray(this.value, value) >= 0 ||
94                                         jQuery.inArray(this.name, value) >= 0);
95
96                         else if ( jQuery.nodeName( this, "select" ) ) {
97                                 var values = jQuery.makeArray(value);
98
99                                 jQuery( "option", this ).each(function(){
100                                         this.selected = (jQuery.inArray( this.value, values ) >= 0 ||
101                                                 jQuery.inArray( this.text, values ) >= 0);
102                                 });
103
104                                 if ( !values.length )
105                                         this.selectedIndex = -1;
106
107                         } else
108                                 this.value = value;
109                 });
110         }
111 });
112
113 jQuery.each({
114         removeAttr: function( name ) {
115                 jQuery.attr( this, name, "" );
116                 if (this.nodeType == 1)
117                         this.removeAttribute( name );
118         },
119
120         addClass: function( classNames ) {
121                 jQuery.className.add( this, classNames );
122         },
123
124         removeClass: function( classNames ) {
125                 jQuery.className.remove( this, classNames );
126         },
127
128         toggleClass: function( classNames, state ) {
129                 var type = typeof classNames;
130                 if ( type === "string" ) {
131                         // toggle individual class names
132                         var isBool = typeof state === "boolean", className, i = 0,
133                                 classNames = classNames.split( /\s+/ );
134                         while ( (className = classNames[ i++ ]) ) {
135                                 // check each className given, space seperated list
136                                 state = isBool ? state : !jQuery.className.has( this, className );
137                                 jQuery.className[ state ? "add" : "remove" ]( this, className );
138                         }
139                 } else if ( type === "undefined" || type === "boolean" ) {
140                         if ( this.className ) {
141                                 // store className if set
142                                 jQuery.data( this, "__className__", this.className );
143                         }
144                         // toggle whole className
145                         this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
146                 }
147         }
148 }, function(name, fn){
149         jQuery.fn[ name ] = function(){
150                 return this.each( fn, arguments );
151         };
152 });
153
154 jQuery.extend({
155         className: {
156                 // internal only, use addClass("class")
157                 add: function( elem, classNames ) {
158                         jQuery.each((classNames || "").split(/\s+/), function(i, className){
159                                 if ( elem.nodeType == 1 && !jQuery.className.has( elem.className, className ) )
160                                         elem.className += (elem.className ? " " : "") + className;
161                         });
162                 },
163
164                 // internal only, use removeClass("class")
165                 remove: function( elem, classNames ) {
166                         if (elem.nodeType == 1)
167                                 elem.className = classNames !== undefined ?
168                                         jQuery.grep(elem.className.split(/\s+/), function(className){
169                                                 return !jQuery.className.has( classNames, className );
170                                         }).join(" ") :
171                                         "";
172                 },
173
174                 // internal only, use hasClass("class")
175                 has: function( elem, className ) {
176                         return elem && jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1;
177                 }
178         },
179
180         attr: function( elem, name, value ) {
181                 // don't set attributes on text and comment nodes
182                 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
183                         return undefined;
184
185                 var notxml = !elem.tagName || !jQuery.isXMLDoc( elem ),
186                         // Whether we are setting (or getting)
187                         set = value !== undefined;
188
189                 // Try to normalize/fix the name
190                 name = notxml && jQuery.props[ name ] || name;
191
192                 // Only do all the following if this is a node (faster for style)
193                 if ( elem.tagName ) {
194
195                         // These attributes require special treatment
196                         var special = /href|src|style/.test( name );
197
198                         // Safari mis-reports the default selected property of a hidden option
199                         // Accessing the parent's selectedIndex property fixes it
200                         if ( name == "selected" && elem.parentNode )
201                                 elem.parentNode.selectedIndex;
202
203                         // If applicable, access the attribute via the DOM 0 way
204                         if ( name in elem && notxml && !special ) {
205                                 if ( set ){
206                                         // We can't allow the type property to be changed (since it causes problems in IE)
207                                         if ( name == "type" && elem.nodeName.match(/(button|input)/i) && elem.parentNode )
208                                                 throw "type property can't be changed";
209
210                                         elem[ name ] = value;
211                                 }
212
213                                 // browsers index elements by id/name on forms, give priority to attributes.
214                                 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
215                                         return elem.getAttributeNode( name ).nodeValue;
216
217                                 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
218                                 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
219                                 if ( name == "tabIndex" ) {
220                                         var attributeNode = elem.getAttributeNode( "tabIndex" );
221                                         return attributeNode && attributeNode.specified
222                                                 ? attributeNode.value
223                                                 : elem.nodeName.match(/(button|input|object|select|textarea)/i)
224                                                         ? 0
225                                                         : elem.nodeName.match(/^(a|area)$/i) && elem.href
226                                                                 ? 0
227                                                                 : undefined;
228                                 }
229
230                                 return elem[ name ];
231                         }
232
233                         if ( !jQuery.support.style && notxml && name == "style" ) {
234                                 if ( set )
235                                         elem.style.cssText = "" + value;
236
237                                 return elem.style.cssText;
238                         }
239
240                         if ( set )
241                                 // convert the value to a string (all browsers do this but IE) see #1070
242                                 elem.setAttribute( name, "" + value );
243
244                         var attr = !jQuery.support.hrefNormalized && notxml && special
245                                         // Some attributes require a special call on IE
246                                         ? elem.getAttribute( name, 2 )
247                                         : elem.getAttribute( name );
248
249                         // Non-existent attributes return null, we normalize to undefined
250                         return attr === null ? undefined : attr;
251                 }
252
253                 // elem is actually elem.style ... set the style
254                 // Using attr for specific style information is now deprecated. Use style insead.
255                 return jQuery.style(elem, name, value);
256         }
257 });