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