3 var rclass = /[\n\t\r]/g,
6 rspecialurl = /^(?:href|src|style)$/,
7 rtype = /^(?:button|input)$/i,
8 rfocusable = /^(?:button|input|object|select|textarea)$/i,
9 rclickable = /^a(?:rea)?$/i,
10 rradiocheck = /^(?:radio|checkbox)$/i;
16 maxlength: "maxLength",
17 cellspacing: "cellSpacing",
22 frameborder: "frameBorder"
26 attr: function( name, value ) {
27 return jQuery.access( this, name, value, true, jQuery.attr );
30 removeAttr: function( name, fn ) {
31 return this.each(function(){
32 jQuery.attr( this, name, "" );
33 if ( this.nodeType === 1 ) {
34 this.removeAttribute( name );
39 addClass: function( value ) {
40 if ( jQuery.isFunction(value) ) {
41 return this.each(function(i) {
42 var self = jQuery(this);
43 self.addClass( value.call(this, i, self.attr("class")) );
47 if ( value && typeof value === "string" ) {
48 var classNames = (value || "").split( rspaces );
50 for ( var i = 0, l = this.length; i < l; i++ ) {
53 if ( elem.nodeType === 1 ) {
54 if ( !elem.className ) {
55 elem.className = value;
58 var className = " " + elem.className + " ",
59 setClass = elem.className;
61 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
62 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
63 setClass += " " + classNames[c];
66 elem.className = jQuery.trim( setClass );
75 removeClass: function( value ) {
76 if ( jQuery.isFunction(value) ) {
77 return this.each(function(i) {
78 var self = jQuery(this);
79 self.removeClass( value.call(this, i, self.attr("class")) );
83 if ( (value && typeof value === "string") || value === undefined ) {
84 var classNames = (value || "").split( rspaces );
86 for ( var i = 0, l = this.length; i < l; i++ ) {
89 if ( elem.nodeType === 1 && elem.className ) {
91 var className = (" " + elem.className + " ").replace(rclass, " ");
92 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
93 className = className.replace(" " + classNames[c] + " ", " ");
95 elem.className = jQuery.trim( className );
107 toggleClass: function( value, stateVal ) {
108 var type = typeof value,
109 isBool = typeof stateVal === "boolean";
111 if ( jQuery.isFunction( value ) ) {
112 return this.each(function(i) {
113 var self = jQuery(this);
114 self.toggleClass( value.call(this, i, self.attr("class"), stateVal), stateVal );
118 return this.each(function() {
119 if ( type === "string" ) {
120 // toggle individual class names
123 self = jQuery( this ),
125 classNames = value.split( rspaces );
127 while ( (className = classNames[ i++ ]) ) {
128 // check each className given, space seperated list
129 state = isBool ? state : !self.hasClass( className );
130 self[ state ? "addClass" : "removeClass" ]( className );
133 } else if ( type === "undefined" || type === "boolean" ) {
134 if ( this.className ) {
135 // store className if set
136 jQuery._data( this, "__className__", this.className );
139 // toggle whole className
140 this.className = this.className || value === false ? "" : jQuery._data( this, "__className__" ) || "";
145 hasClass: function( selector ) {
146 var className = " " + selector + " ";
147 for ( var i = 0, l = this.length; i < l; i++ ) {
148 if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
156 val: function( value ) {
157 if ( !arguments.length ) {
161 if ( jQuery.nodeName( elem, "option" ) ) {
162 // attributes.value is undefined in Blackberry 4.7 but
163 // uses .value. See #6932
164 var val = elem.attributes.value;
165 return !val || val.specified ? elem.value : elem.text;
168 // We need to handle select boxes special
169 if ( jQuery.nodeName( elem, "select" ) ) {
170 var index = elem.selectedIndex,
172 options = elem.options,
173 one = elem.type === "select-one";
175 // Nothing was selected
180 // Loop through all the selected options
181 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
182 var option = options[ i ];
184 // Don't return options that are disabled or in a disabled optgroup
185 if ( option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) &&
186 (!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) {
188 // Get the specific value for the option
189 value = jQuery(option).val();
191 // We don't need an array for one selects
196 // Multi-Selects return an array
197 values.push( value );
201 // Fixes Bug #2551 -- select.val() broken in IE after form.reset()
202 if ( one && !values.length && options.length ) {
203 return jQuery( options[ index ] ).val();
209 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
210 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
211 return elem.getAttribute("value") === null ? "on" : elem.value;
214 // Everything else, we just grab the value
215 return (elem.value || "").replace(rreturn, "");
222 var isFunction = jQuery.isFunction(value);
224 return this.each(function(i) {
225 var self = jQuery(this), val = value;
227 if ( this.nodeType !== 1 ) {
232 val = value.call(this, i, self.val());
235 // Treat null/undefined as ""; convert numbers to string
238 } else if ( typeof val === "number" ) {
240 } else if ( jQuery.isArray(val) ) {
241 val = jQuery.map(val, function (value) {
242 return value == null ? "" : value + "";
246 if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
247 this.checked = jQuery.inArray( self.val(), val ) >= 0;
249 } else if ( jQuery.nodeName( this, "select" ) ) {
250 var values = jQuery.makeArray(val);
252 jQuery( "option", this ).each(function() {
253 this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
256 if ( !values.length ) {
257 this.selectedIndex = -1;
279 attr: function( elem, name, value, pass ) {
280 // don't get/set attributes on text, comment and attribute nodes
281 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || elem.nodeType === 2 ) {
285 if ( pass && name in jQuery.attrFn ) {
286 return jQuery(elem)[name](value);
289 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
290 // Whether we are setting (or getting)
291 set = value !== undefined;
293 // Try to normalize/fix the name
294 name = notxml && jQuery.props[ name ] || name;
296 // Only do all the following if this is a node (faster for style)
297 if ( elem.nodeType === 1 ) {
298 // These attributes require special treatment
299 var special = rspecialurl.test( name );
301 // Safari mis-reports the default selected property of an option
302 // Accessing the parent's selectedIndex property fixes it
303 if ( name === "selected" && !jQuery.support.optSelected ) {
304 var parent = elem.parentNode;
306 parent.selectedIndex;
308 // Make sure that it also works with optgroups, see #5701
309 if ( parent.parentNode ) {
310 parent.parentNode.selectedIndex;
315 // If applicable, access the attribute via the DOM 0 way
316 // 'in' checks fail in Blackberry 4.7 #6931
317 if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
319 // We can't allow the type property to be changed (since it causes problems in IE)
320 if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
321 jQuery.error( "type property can't be changed" );
324 if ( value === null ) {
325 if ( elem.nodeType === 1 ) {
326 elem.removeAttribute( name );
330 elem[ name ] = value;
334 // browsers index elements by id/name on forms, give priority to attributes.
335 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
336 return elem.getAttributeNode( name ).nodeValue;
339 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
340 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
341 if ( name === "tabIndex" ) {
342 var attributeNode = elem.getAttributeNode( "tabIndex" );
344 return attributeNode && attributeNode.specified ?
345 attributeNode.value :
346 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
354 if ( !jQuery.support.style && notxml && name === "style" ) {
356 elem.style.cssText = "" + value;
359 return elem.style.cssText;
363 // convert the value to a string (all browsers do this but IE) see #1070
364 elem.setAttribute( name, "" + value );
367 // Ensure that missing attributes return undefined
368 // Blackberry 4.7 returns "" from getAttribute #6938
369 if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
373 var attr = !jQuery.support.hrefNormalized && notxml && special ?
374 // Some attributes require a special call on IE
375 elem.getAttribute( name, 2 ) :
376 elem.getAttribute( name );
378 // Non-existent attributes return null, we normalize to undefined
379 return attr === null ? undefined : attr;
381 // Handle everything which isn't a DOM element node
383 elem[ name ] = value;