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 + " ", setClass = elem.className;
59 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
60 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
61 setClass += " " + classNames[c];
64 elem.className = jQuery.trim( setClass );
73 removeClass: function( value ) {
74 if ( jQuery.isFunction(value) ) {
75 return this.each(function(i) {
76 var self = jQuery(this);
77 self.removeClass( value.call(this, i, self.attr("class")) );
81 if ( (value && typeof value === "string") || value === undefined ) {
82 var classNames = (value || "").split( rspaces );
84 for ( var i = 0, l = this.length; i < l; i++ ) {
87 if ( elem.nodeType === 1 && elem.className ) {
89 var className = (" " + elem.className + " ").replace(rclass, " ");
90 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
91 className = className.replace(" " + classNames[c] + " ", " ");
93 elem.className = jQuery.trim( className );
105 toggleClass: function( value, stateVal ) {
106 var type = typeof value, isBool = typeof stateVal === "boolean";
108 if ( jQuery.isFunction( value ) ) {
109 return this.each(function(i) {
110 var self = jQuery(this);
111 self.toggleClass( value.call(this, i, self.attr("class"), stateVal), stateVal );
115 return this.each(function() {
116 if ( type === "string" ) {
117 // toggle individual class names
118 var className, i = 0, self = jQuery(this),
120 classNames = value.split( rspaces );
122 while ( (className = classNames[ i++ ]) ) {
123 // check each className given, space seperated list
124 state = isBool ? state : !self.hasClass( className );
125 self[ state ? "addClass" : "removeClass" ]( className );
128 } else if ( type === "undefined" || type === "boolean" ) {
129 if ( this.className ) {
130 // store className if set
131 jQuery.data( this, "__className__", this.className );
134 // toggle whole className
135 this.className = this.className || value === false ? "" : jQuery.data( this, "__className__" ) || "";
140 hasClass: function( selector ) {
141 var className = " " + selector + " ";
142 for ( var i = 0, l = this.length; i < l; i++ ) {
143 if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
151 val: function( value ) {
152 if ( !arguments.length ) {
156 if ( jQuery.nodeName( elem, "option" ) ) {
157 // attributes.value is undefined in Blackberry 4.7 but
158 // uses .value. See #6932
159 var val = elem.attributes.value;
160 return !val || val.specified ? elem.value : elem.text;
163 // We need to handle select boxes special
164 if ( jQuery.nodeName( elem, "select" ) ) {
165 var index = elem.selectedIndex,
167 options = elem.options,
168 one = elem.type === "select-one";
170 // Nothing was selected
175 // Loop through all the selected options
176 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
177 var option = options[ i ];
179 // Don't return options that are disabled or in a disabled optgroup
180 if ( option.selected && (jQuery.support.optDisabled ? !option.disabled : option.getAttribute("disabled") === null) &&
181 (!option.parentNode.disabled || !jQuery.nodeName( option.parentNode, "optgroup" )) ) {
183 // Get the specific value for the option
184 value = jQuery(option).val();
186 // We don't need an array for one selects
191 // Multi-Selects return an array
192 values.push( value );
199 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
200 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
201 return elem.getAttribute("value") === null ? "on" : elem.value;
205 // Everything else, we just grab the value
206 return (elem.value || "").replace(rreturn, "");
213 var isFunction = jQuery.isFunction(value);
215 return this.each(function(i) {
216 var self = jQuery(this), val = value;
218 if ( this.nodeType !== 1 ) {
223 val = value.call(this, i, self.val());
226 // Treat null/undefined as ""; convert numbers to string
229 } else if ( typeof val === "number" ) {
231 } else if ( jQuery.isArray(val) ) {
232 val = jQuery.map(val, function (value) {
233 return value == null ? "" : value + "";
237 if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
238 this.checked = jQuery.inArray( self.val(), val ) >= 0;
240 } else if ( jQuery.nodeName( this, "select" ) ) {
241 var values = jQuery.makeArray(val);
243 jQuery( "option", this ).each(function() {
244 this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
247 if ( !values.length ) {
248 this.selectedIndex = -1;
270 attr: function( elem, name, value, pass ) {
271 // don't set attributes on text and comment nodes
272 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
276 if ( pass && name in jQuery.attrFn ) {
277 return jQuery(elem)[name](value);
280 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
281 // Whether we are setting (or getting)
282 set = value !== undefined;
284 // Try to normalize/fix the name
285 name = notxml && jQuery.props[ name ] || name;
287 // Only do all the following if this is a node (faster for style)
288 if ( elem.nodeType === 1 ) {
289 // These attributes require special treatment
290 var special = rspecialurl.test( name );
292 // Safari mis-reports the default selected property of an option
293 // Accessing the parent's selectedIndex property fixes it
294 if ( name === "selected" && !jQuery.support.optSelected ) {
295 var parent = elem.parentNode;
297 parent.selectedIndex;
299 // Make sure that it also works with optgroups, see #5701
300 if ( parent.parentNode ) {
301 parent.parentNode.selectedIndex;
306 // If applicable, access the attribute via the DOM 0 way
307 // 'in' checks fail in Blackberry 4.7 #6931
308 if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
310 // We can't allow the type property to be changed (since it causes problems in IE)
311 if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
312 jQuery.error( "type property can't be changed" );
315 if ( value === null ) {
316 if ( elem.nodeType === 1 ) {
317 elem.removeAttribute( name );
321 elem[ name ] = value;
325 // browsers index elements by id/name on forms, give priority to attributes.
326 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
327 return elem.getAttributeNode( name ).nodeValue;
330 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
331 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
332 if ( name === "tabIndex" ) {
333 var attributeNode = elem.getAttributeNode( "tabIndex" );
335 return attributeNode && attributeNode.specified ?
336 attributeNode.value :
337 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
345 if ( !jQuery.support.style && notxml && name === "style" ) {
347 elem.style.cssText = "" + value;
350 return elem.style.cssText;
354 // convert the value to a string (all browsers do this but IE) see #1070
355 elem.setAttribute( name, "" + value );
358 // Ensure that missing attributes return undefined
359 // Blackberry 4.7 returns "" from getAttribute #6938
360 if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
364 var attr = !jQuery.support.hrefNormalized && notxml && special ?
365 // Some attributes require a special call on IE
366 elem.getAttribute( name, 2 ) :
367 elem.getAttribute( name );
369 // Non-existent attributes return null, we normalize to undefined
370 return attr === null ? undefined : attr;