1 var rclass = /[\n\t]/g,
4 rspecialurl = /href|src|style/,
5 rtype = /(button|input)/i,
6 rfocusable = /(button|input|object|select|textarea)/i,
7 rclickable = /^(a|area)$/i,
8 rradiocheck = /radio|checkbox/;
11 attr: function( name, value ) {
12 return access( this, name, value, true, jQuery.attr );
15 removeAttr: function( name, fn ) {
16 return this.each(function(){
17 jQuery.attr( this, name, "" );
18 if ( this.nodeType === 1 ) {
19 this.removeAttribute( name );
24 addClass: function( value ) {
25 if ( jQuery.isFunction(value) ) {
26 return this.each(function(i) {
27 var self = jQuery(this);
28 self.addClass( value.call(this, i, self.attr("class")) );
32 if ( value && typeof value === "string" ) {
33 var classNames = (value || "").split( rspace );
35 for ( var i = 0, l = this.length; i < l; i++ ) {
38 if ( elem.nodeType === 1 ) {
39 if ( !elem.className ) {
40 elem.className = value;
43 var className = " " + elem.className + " ";
44 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
45 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
46 elem.className += " " + classNames[c];
57 removeClass: function( value ) {
58 if ( jQuery.isFunction(value) ) {
59 return this.each(function(i) {
60 var self = jQuery(this);
61 self.removeClass( value.call(this, i, self.attr("class")) );
65 if ( (value && typeof value === "string") || value === undefined ) {
66 var classNames = (value || "").split(rspace);
68 for ( var i = 0, l = this.length; i < l; i++ ) {
71 if ( elem.nodeType === 1 && elem.className ) {
73 var className = (" " + elem.className + " ").replace(rclass, " ");
74 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
75 className = className.replace(" " + classNames[c] + " ", " ");
77 elem.className = className.substring(1, className.length - 1);
89 toggleClass: function( value, stateVal ) {
90 var type = typeof value, isBool = typeof stateVal === "boolean";
92 if ( jQuery.isFunction( value ) ) {
93 return this.each(function(i) {
94 var self = jQuery(this);
95 self.toggleClass( value.call(this, i, self.attr("class"), stateVal), stateVal );
99 return this.each(function() {
100 if ( type === "string" ) {
101 // toggle individual class names
102 var className, i = 0, self = jQuery(this),
104 classNames = value.split( rspace );
106 while ( (className = classNames[ i++ ]) ) {
107 // check each className given, space seperated list
108 state = isBool ? state : !self.hasClass( className );
109 self[ state ? "addClass" : "removeClass" ]( className );
112 } else if ( type === "undefined" || type === "boolean" ) {
113 if ( this.className ) {
114 // store className if set
115 jQuery.data( this, "__className__", this.className );
118 // toggle whole className
119 this.className = this.className || value === false ? "" : jQuery.data( this, "__className__" ) || "";
124 hasClass: function( selector ) {
125 var className = " " + selector + " ";
126 for ( var i = 0, l = this.length; i < l; i++ ) {
127 if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
135 val: function( value ) {
136 if ( value === undefined ) {
140 if ( jQuery.nodeName( elem, "option" ) ) {
141 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
144 // We need to handle select boxes special
145 if ( jQuery.nodeName( elem, "select" ) ) {
146 var index = elem.selectedIndex,
148 options = elem.options,
149 one = elem.type === "select-one";
151 // Nothing was selected
156 // Loop through all the selected options
157 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
158 var option = options[ i ];
160 if ( option.selected ) {
161 // Get the specifc value for the option
162 value = jQuery(option).val();
164 // We don't need an array for one selects
169 // Multi-Selects return an array
170 values.push( value );
177 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
178 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
179 return elem.getAttribute("value") === null ? "on" : elem.value;
183 // Everything else, we just grab the value
184 return (elem.value || "").replace(rreturn, "");
191 var isFunction = jQuery.isFunction(value);
193 return this.each(function(i) {
194 var self = jQuery(this), val = value;
196 if ( this.nodeType !== 1 ) {
201 val = value.call(this, i, self.val());
204 // Typecast each time if the value is a Function and the appended
205 // value is therefore different each time.
206 if ( typeof val === "number" ) {
210 if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
211 this.checked = jQuery.inArray( self.val(), val ) >= 0;
213 } else if ( jQuery.nodeName( this, "select" ) ) {
214 var values = jQuery.makeArray(val);
216 jQuery( "option", this ).each(function() {
217 this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
220 if ( !values.length ) {
221 this.selectedIndex = -1;
243 attr: function( elem, name, value, pass ) {
244 // don't set attributes on text and comment nodes
245 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
249 if ( pass && name in jQuery.attrFn ) {
250 return jQuery(elem)[name](value);
253 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
254 // Whether we are setting (or getting)
255 set = value !== undefined;
257 // Try to normalize/fix the name
258 name = notxml && jQuery.props[ name ] || name;
260 // Only do all the following if this is a node (faster for style)
261 if ( elem.nodeType === 1 ) {
262 // These attributes require special treatment
263 var special = rspecialurl.test( name );
265 // Safari mis-reports the default selected property of an option
266 // Accessing the parent's selectedIndex property fixes it
267 if ( name === "selected" && !jQuery.support.optSelected ) {
268 var parent = elem.parentNode;
270 parent.selectedIndex;
272 // Make sure that it also works with optgroups, see #5701
273 if ( parent.parentNode ) {
274 parent.parentNode.selectedIndex;
279 // If applicable, access the attribute via the DOM 0 way
280 if ( name in elem && notxml && !special ) {
282 // We can't allow the type property to be changed (since it causes problems in IE)
283 if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
284 throw "type property can't be changed";
287 elem[ name ] = value;
290 // browsers index elements by id/name on forms, give priority to attributes.
291 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
292 return elem.getAttributeNode( name ).nodeValue;
295 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
296 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
297 if ( name === "tabIndex" ) {
298 var attributeNode = elem.getAttributeNode( "tabIndex" );
300 return attributeNode && attributeNode.specified ?
301 attributeNode.value :
302 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
310 if ( !jQuery.support.style && notxml && name === "style" ) {
312 elem.style.cssText = "" + value;
315 return elem.style.cssText;
319 // convert the value to a string (all browsers do this but IE) see #1070
320 elem.setAttribute( name, "" + value );
323 var attr = !jQuery.support.hrefNormalized && notxml && special ?
324 // Some attributes require a special call on IE
325 elem.getAttribute( name, 2 ) :
326 elem.getAttribute( name );
328 // Non-existent attributes return null, we normalize to undefined
329 return attr === null ? undefined : attr;
332 // elem is actually elem.style ... set the style
333 // Using attr for specific style information is now deprecated. Use style insead.
334 return jQuery.style( elem, name, value );