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|area)$/i,
10 rradiocheck = /radio|checkbox/;
13 attr: function( name, value ) {
14 return jQuery.access( this, name, value, true, jQuery.attr );
17 removeAttr: function( name, fn ) {
18 return this.each(function(){
19 jQuery.attr( this, name, "" );
20 if ( this.nodeType === 1 ) {
21 this.removeAttribute( name );
26 addClass: function( value ) {
27 if ( jQuery.isFunction(value) ) {
28 return this.each(function(i) {
29 var self = jQuery(this);
30 self.addClass( value.call(this, i, self.attr("class")) );
34 if ( value && typeof value === "string" ) {
35 var classNames = (value || "").split( rspace );
37 for ( var i = 0, l = this.length; i < l; i++ ) {
40 if ( elem.nodeType === 1 ) {
41 if ( !elem.className ) {
42 elem.className = value;
45 var className = " " + elem.className + " ", setClass = elem.className;
46 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
47 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
48 setClass += " " + classNames[c];
51 elem.className = jQuery.trim( setClass );
60 removeClass: function( value ) {
61 if ( jQuery.isFunction(value) ) {
62 return this.each(function(i) {
63 var self = jQuery(this);
64 self.removeClass( value.call(this, i, self.attr("class")) );
68 if ( (value && typeof value === "string") || value === undefined ) {
69 var classNames = (value || "").split(rspace);
71 for ( var i = 0, l = this.length; i < l; i++ ) {
74 if ( elem.nodeType === 1 && elem.className ) {
76 var className = (" " + elem.className + " ").replace(rclass, " ");
77 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
78 className = className.replace(" " + classNames[c] + " ", " ");
80 elem.className = jQuery.trim( className );
92 toggleClass: function( value, stateVal ) {
93 var type = typeof value, isBool = typeof stateVal === "boolean";
95 if ( jQuery.isFunction( value ) ) {
96 return this.each(function(i) {
97 var self = jQuery(this);
98 self.toggleClass( value.call(this, i, self.attr("class"), stateVal), stateVal );
102 return this.each(function() {
103 if ( type === "string" ) {
104 // toggle individual class names
105 var className, i = 0, self = jQuery(this),
107 classNames = value.split( rspace );
109 while ( (className = classNames[ i++ ]) ) {
110 // check each className given, space seperated list
111 state = isBool ? state : !self.hasClass( className );
112 self[ state ? "addClass" : "removeClass" ]( className );
115 } else if ( type === "undefined" || type === "boolean" ) {
116 if ( this.className ) {
117 // store className if set
118 jQuery.data( this, "__className__", this.className );
121 // toggle whole className
122 this.className = this.className || value === false ? "" : jQuery.data( this, "__className__" ) || "";
127 hasClass: function( selector ) {
128 var className = " " + selector + " ";
129 for ( var i = 0, l = this.length; i < l; i++ ) {
130 if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
138 val: function( value ) {
139 if ( value === undefined ) {
143 if ( jQuery.nodeName( elem, "option" ) ) {
144 // attributes.value is undefined in Blackberry 4.7 but
145 // uses .value. See #6932
146 var val = elem.attributes.value;
147 return !val || val.specified ? elem.value : elem.text;
150 // We need to handle select boxes special
151 if ( jQuery.nodeName( elem, "select" ) ) {
152 var index = elem.selectedIndex,
154 options = elem.options,
155 one = elem.type === "select-one";
157 // Nothing was selected
162 // Loop through all the selected options
163 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
164 var option = options[ i ];
166 if ( option.selected ) {
167 // Get the specifc value for the option
168 value = jQuery(option).val();
170 // We don't need an array for one selects
175 // Multi-Selects return an array
176 values.push( value );
183 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
184 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
185 return elem.getAttribute("value") === null ? "on" : elem.value;
189 // Everything else, we just grab the value
190 return (elem.value || "").replace(rreturn, "");
197 var isFunction = jQuery.isFunction(value);
199 return this.each(function(i) {
200 var self = jQuery(this), val = value;
202 if ( this.nodeType !== 1 ) {
207 val = value.call(this, i, self.val());
210 // Typecast each time if the value is a Function and the appended
211 // value is therefore different each time.
212 if ( typeof val === "number" ) {
216 if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
217 this.checked = jQuery.inArray( self.val(), val ) >= 0;
219 } else if ( jQuery.nodeName( this, "select" ) ) {
220 var values = jQuery.makeArray(val);
222 jQuery( "option", this ).each(function() {
223 this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
226 if ( !values.length ) {
227 this.selectedIndex = -1;
249 attr: function( elem, name, value, pass ) {
250 // don't set attributes on text and comment nodes
251 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
255 if ( pass && name in jQuery.attrFn ) {
256 return jQuery(elem)[name](value);
259 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
260 // Whether we are setting (or getting)
261 set = value !== undefined;
263 // Try to normalize/fix the name
264 name = notxml && jQuery.props[ name ] || name;
266 // Only do all the following if this is a node (faster for style)
267 if ( elem.nodeType === 1 ) {
268 // These attributes require special treatment
269 var special = rspecialurl.test( name );
271 // Safari mis-reports the default selected property of an option
272 // Accessing the parent's selectedIndex property fixes it
273 if ( name === "selected" && !jQuery.support.optSelected ) {
274 var parent = elem.parentNode;
276 parent.selectedIndex;
278 // Make sure that it also works with optgroups, see #5701
279 if ( parent.parentNode ) {
280 parent.parentNode.selectedIndex;
285 // If applicable, access the attribute via the DOM 0 way
286 // 'in' checks fail in Blackberry 4.7 #6931
287 if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
289 // We can't allow the type property to be changed (since it causes problems in IE)
290 if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
291 jQuery.error( "type property can't be changed" );
294 elem[ name ] = value;
297 // browsers index elements by id/name on forms, give priority to attributes.
298 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
299 return elem.getAttributeNode( name ).nodeValue;
302 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
303 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
304 if ( name === "tabIndex" ) {
305 var attributeNode = elem.getAttributeNode( "tabIndex" );
307 return attributeNode && attributeNode.specified ?
308 attributeNode.value :
309 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
317 if ( !jQuery.support.style && notxml && name === "style" ) {
319 elem.style.cssText = "" + value;
322 return elem.style.cssText;
326 // convert the value to a string (all browsers do this but IE) see #1070
327 elem.setAttribute( name, "" + value );
330 // Ensure that missing attributes return undefined
331 // Blackberry 4.7 returns "" from getAttribute #6938
332 if ( !elem.attributes[ name ] && !elem.hasAttribute( name ) ) {
336 var attr = !jQuery.support.hrefNormalized && notxml && special ?
337 // Some attributes require a special call on IE
338 elem.getAttribute( name, 2 ) :
339 elem.getAttribute( name );
341 // Non-existent attributes return null, we normalize to undefined
342 return attr === null ? undefined : attr;