3 var rclass = /[\n\t]/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 );
204 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
205 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
206 return elem.getAttribute("value") === null ? "on" : elem.value;
210 // Everything else, we just grab the value
211 return (elem.value || "").replace(rreturn, "");
218 var isFunction = jQuery.isFunction(value);
220 return this.each(function(i) {
221 var self = jQuery(this), val = value;
223 if ( this.nodeType !== 1 ) {
228 val = value.call(this, i, self.val());
231 // Treat null/undefined as ""; convert numbers to string
234 } else if ( typeof val === "number" ) {
236 } else if ( jQuery.isArray(val) ) {
237 val = jQuery.map(val, function (value) {
238 return value == null ? "" : value + "";
242 if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
243 this.checked = jQuery.inArray( self.val(), val ) >= 0;
245 } else if ( jQuery.nodeName( this, "select" ) ) {
246 var values = jQuery.makeArray(val);
248 jQuery( "option", this ).each(function() {
249 this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
252 if ( !values.length ) {
253 this.selectedIndex = -1;
275 attr: function( elem, name, value, pass ) {
276 // don't set attributes on text and comment nodes
277 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
281 if ( pass && name in jQuery.attrFn ) {
282 return jQuery(elem)[name](value);
285 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
286 // Whether we are setting (or getting)
287 set = value !== undefined;
289 // Try to normalize/fix the name
290 name = notxml && jQuery.props[ name ] || name;
292 // These attributes require special treatment
293 var special = rspecialurl.test( name );
295 // Safari mis-reports the default selected property of an option
296 // Accessing the parent's selectedIndex property fixes it
297 if ( name === "selected" && !jQuery.support.optSelected ) {
298 var parent = elem.parentNode;
300 parent.selectedIndex;
302 // Make sure that it also works with optgroups, see #5701
303 if ( parent.parentNode ) {
304 parent.parentNode.selectedIndex;
309 // If applicable, access the attribute via the DOM 0 way
310 // 'in' checks fail in Blackberry 4.7 #6931
311 if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
313 // We can't allow the type property to be changed (since it causes problems in IE)
314 if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
315 jQuery.error( "type property can't be changed" );
318 if ( value === null ) {
319 if ( elem.nodeType === 1 ) {
320 elem.removeAttribute( name );
324 elem[ name ] = value;
328 // browsers index elements by id/name on forms, give priority to attributes.
329 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
330 return elem.getAttributeNode( name ).nodeValue;
333 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
334 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
335 if ( name === "tabIndex" ) {
336 var attributeNode = elem.getAttributeNode( "tabIndex" );
338 return attributeNode && attributeNode.specified ?
339 attributeNode.value :
340 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
348 if ( !jQuery.support.style && notxml && name === "style" ) {
350 elem.style.cssText = "" + value;
353 return elem.style.cssText;
357 // convert the value to a string (all browsers do this but IE) see #1070
358 elem.setAttribute( name, "" + value );
361 // Ensure that missing attributes return undefined
362 // Blackberry 4.7 returns "" from getAttribute #6938
363 if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
367 var attr = !jQuery.support.hrefNormalized && notxml && special ?
368 // Some attributes require a special call on IE
369 elem.getAttribute( name, 2 ) :
370 elem.getAttribute( name );
372 // Non-existent attributes return null, we normalize to undefined
373 return attr === null ? undefined : attr;