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