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