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