Handle changing form attributes correctly when there is a child element with the...
[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" ) {
125                                         val += ''; 
126                                 }
127                         }
128                         
129                         if ( this.nodeType != 1 ) {
130                                 return;
131                         }
132                         if ( jQuery.isArray(val) && /radio|checkbox/.test( this.type ) ) {
133                                 this.checked = jQuery.inArray(this.value || this.name, val) >= 0;
134                         }
135                         else if ( jQuery.nodeName( this, "select" ) ) {
136                                 var values = jQuery.makeArray(val);
137
138                                 jQuery( "option", this ).each(function(){
139                                         this.selected = jQuery.inArray( this.value || this.text, values ) >= 0;
140                                 });
141
142                                 if ( !values.length ) {
143                                         this.selectedIndex = -1;
144                                 }
145                         } else {
146                                 this.value = val;
147                         }
148                 });
149         }
150 });
151
152 jQuery.each({
153         removeAttr: function( name ) {
154                 jQuery.attr( this, name, "" );
155                 if (this.nodeType == 1) {
156                         this.removeAttribute( name );
157                 }
158         },
159
160         toggleClass: function( classNames, state ) {
161                 var type = typeof classNames;
162                 if ( type === "string" ) {
163                         // toggle individual class names
164                         var isBool = typeof state === "boolean", className, i = 0,
165                                 classNames = classNames.split( /\s+/ );
166                         while ( (className = classNames[ i++ ]) ) {
167                                 // check each className given, space seperated list
168                                 state = isBool ? state : !jQuery(this).hasClass( className );
169                                 jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
170                         }
171                 } else if ( type === "undefined" || type === "boolean" ) {
172                         if ( this.className ) {
173                                 // store className if set
174                                 jQuery.data( this, "__className__", this.className );
175                         }
176                         // toggle whole className
177                         this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
178                 }
179         }
180 }, function(name, fn){
181         jQuery.fn[ name ] = function(){
182                 return this.each( fn, arguments );
183         };
184 });
185
186 jQuery.extend({
187         attr: function( elem, name, value ) {
188                 // don't set attributes on text and comment nodes
189                 if (!elem || elem.nodeType == 3 || elem.nodeType == 8) {
190                         return undefined;
191                 }
192                 if ( name in jQuery.fn && name !== "attr" ) {
193                         return jQuery(elem)[name](value);
194                 }
195                 
196                 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
197                         // Whether we are setting (or getting)
198                         set = value !== undefined;
199
200                 // Try to normalize/fix the name
201                 name = notxml && jQuery.props[ name ] || name;
202
203                 // Only do all the following if this is a node (faster for style)
204                 if ( elem.nodeType === 1 ) {
205
206                         // These attributes require special treatment
207                         var special = /href|src|style/.test( name );
208
209                         // Safari mis-reports the default selected property of a hidden option
210                         // Accessing the parent's selectedIndex property fixes it
211                         if ( name == "selected" && elem.parentNode ) {
212                                 elem.parentNode.selectedIndex;
213                         }
214                         // If applicable, access the attribute via the DOM 0 way
215                         if ( name in elem && notxml && !special ) {
216                                 if ( set ) {
217                                         // We can't allow the type property to be changed (since it causes problems in IE)
218                                         if ( name == "type" && /(button|input)/i.test(elem.nodeName) && elem.parentNode ) {
219                                                 throw "type property can't be changed";
220                                         }
221                                         // browsers index elements by id/name on forms, give priority to attributes.
222                                         if( jQuery.nodeName( elem, "form" ) ) {
223                                                 // convert the value to a string (all browsers do this but IE) see #1070
224                                                 elem.setAttribute( name, "" + value );
225                                         } else {
226                                                 elem[ name ] = value;
227                                         }
228                                 }
229
230                                 // browsers index elements by id/name on forms, give priority to attributes.
231                                 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
232                                         return elem.getAttributeNode( name ).nodeValue;
233                                 }
234                                 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
235                                 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
236                                 if ( name == "tabIndex" ) {
237                                         var attributeNode = elem.getAttributeNode( "tabIndex" );
238                                         return attributeNode && attributeNode.specified
239                                                 ? attributeNode.value
240                                                 : /(button|input|object|select|textarea)/i.test(elem.nodeName)
241                                                         ? 0
242                                                         : /^(a|area)$/i.test(elem.nodeName) && elem.href
243                                                                 ? 0
244                                                                 : undefined;
245                                 }
246
247                                 return elem[ name ];
248                         }
249
250                         if ( !jQuery.support.style && notxml && name == "style" ) {
251                                 if ( set ) {
252                                         elem.style.cssText = "" + value;
253                                 }
254                                 return elem.style.cssText;
255                         }
256
257                         if ( set ) {
258                                 // convert the value to a string (all browsers do this but IE) see #1070
259                                 elem.setAttribute( name, "" + value );
260                         }
261                         var attr = !jQuery.support.hrefNormalized && notxml && special
262                                         // Some attributes require a special call on IE
263                                         ? elem.getAttribute( name, 2 )
264                                         : elem.getAttribute( name );
265
266                         // Non-existent attributes return null, we normalize to undefined
267                         return attr === null ? undefined : attr;
268                 }
269
270                 // elem is actually elem.style ... set the style
271                 // Using attr for specific style information is now deprecated. Use style insead.
272                 return jQuery.style(elem, name, value);
273         }
274 });