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;
10 attr: function( name, value ) {
11 return access( this, name, value, true, jQuery.attr );
14 addClass: function( value ) {
15 if ( jQuery.isFunction(value) ) {
16 return this.each(function() {
17 jQuery(this).addClass( value.call(this) );
21 if ( value && typeof value === "string" ) {
22 var classNames = (value || "").split( rspace );
24 for ( var i = 0, l = this.length; i < l; i++ ) {
27 if ( elem.nodeType === 1 ) {
28 if ( !elem.className ) {
29 elem.className = value;
32 var className = " " + elem.className + " ";
33 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
34 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
35 elem.className += " " + classNames[c];
46 removeClass: function( value ) {
47 if ( jQuery.isFunction(value) ) {
48 return this.each(function() {
49 jQuery(this).removeClass( value.call(this) );
53 if ( (value && typeof value === "string") || value === undefined ) {
54 var classNames = (value || "").split(rspace);
56 for ( var i = 0, l = this.length; i < l; i++ ) {
59 if ( elem.nodeType === 1 && elem.className ) {
61 var className = (" " + elem.className + " ").replace(rclass, " ");
62 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
63 className = className.replace(" " + classNames[c] + " ", " ");
65 elem.className = className.substring(1, className.length - 1);
77 hasClass: function( selector ) {
78 var className = " " + selector + " ";
79 for ( var i = 0, l = this.length; i < l; i++ ) {
80 if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
88 val: function( value ) {
89 if ( value === undefined ) {
93 if ( jQuery.nodeName( elem, "option" ) ) {
94 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
97 // We need to handle select boxes special
98 if ( jQuery.nodeName( elem, "select" ) ) {
99 var index = elem.selectedIndex,
101 options = elem.options,
102 one = elem.type === "select-one";
104 // Nothing was selected
109 // Loop through all the selected options
110 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
111 var option = options[ i ];
113 if ( option.selected ) {
114 // Get the specifc value for the option
115 value = jQuery(option).val();
117 // We don't need an array for one selects
122 // Multi-Selects return an array
123 values.push( value );
130 // Everything else, we just grab the value
131 return (elem.value || "").replace(rreturn, "");
138 // Typecast once if the value is a number
139 if ( typeof value === "number" ) {
145 return this.each(function() {
146 if ( jQuery.isFunction(value) ) {
147 val = value.call(this);
149 // Typecast each time if the value is a Function and the appended
150 // value is therefore different each time.
151 if ( typeof val === "number" ) {
156 if ( this.nodeType !== 1 ) {
160 if ( jQuery.isArray(val) && /radio|checkbox/.test( this.type ) ) {
161 this.checked = jQuery.inArray( this.value, val ) >= 0;
163 } else if ( jQuery.nodeName( this, "select" ) ) {
164 var values = jQuery.makeArray(val);
166 jQuery( "option", this ).each(function() {
167 this.selected = jQuery.inArray( this.value, values ) >= 0;
170 if ( !values.length ) {
171 this.selectedIndex = -1;
182 removeAttr: function( name ) {
183 jQuery.attr( this, name, "" );
184 if ( this.nodeType === 1 ) {
185 this.removeAttribute( name );
189 toggleClass: function( classNames, state ) {
190 var type = typeof classNames;
192 if ( type === "string" ) {
193 // toggle individual class names
194 var isBool = typeof state === "boolean", className, i = 0,
195 classNames = classNames.split( rspace );
197 while ( (className = classNames[ i++ ]) ) {
198 // check each className given, space seperated list
199 state = isBool ? state : !jQuery(this).hasClass( className );
200 jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
203 } else if ( type === "undefined" || type === "boolean" ) {
204 if ( this.className ) {
205 // store className if set
206 jQuery.data( this, "__className__", this.className );
209 // toggle whole className
210 this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
213 }, function( name, fn ) {
214 jQuery.fn[ name ] = function( val, state ) {
215 if ( jQuery.isFunction( val ) ) {
216 return this.each(function() {
217 jQuery(this)[ name ]( val.call(this), state );
221 return this.each( fn, arguments );
237 attr: function( elem, name, value, pass ) {
238 // don't set attributes on text and comment nodes
239 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
243 if ( pass && name in jQuery.attrFn ) {
244 return jQuery(elem)[name](value);
247 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
248 // Whether we are setting (or getting)
249 set = value !== undefined;
251 // Try to normalize/fix the name
252 name = notxml && jQuery.props[ name ] || name;
254 // Only do all the following if this is a node (faster for style)
255 if ( elem.nodeType === 1 ) {
256 // These attributes require special treatment
257 var special = rspecialurl.test( name );
259 // Safari mis-reports the default selected property of a hidden option
260 // Accessing the parent's selectedIndex property fixes it
261 if ( name === "selected" && elem.parentNode ) {
262 elem.parentNode.selectedIndex;
265 // If applicable, access the attribute via the DOM 0 way
266 if ( name in elem && notxml && !special ) {
268 // We can't allow the type property to be changed (since it causes problems in IE)
269 if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
270 throw "type property can't be changed";
273 elem[ name ] = value;
276 // browsers index elements by id/name on forms, give priority to attributes.
277 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
278 return elem.getAttributeNode( name ).nodeValue;
281 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
282 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
283 if ( name === "tabIndex" ) {
284 var attributeNode = elem.getAttributeNode( "tabIndex" );
286 return attributeNode && attributeNode.specified ?
287 attributeNode.value :
288 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
296 if ( !jQuery.support.style && notxml && name === "style" ) {
298 elem.style.cssText = "" + value;
301 return elem.style.cssText;
305 // convert the value to a string (all browsers do this but IE) see #1070
306 elem.setAttribute( name, "" + value );
309 var attr = !jQuery.support.hrefNormalized && notxml && special ?
310 // Some attributes require a special call on IE
311 elem.getAttribute( name, 2 ) :
312 elem.getAttribute( name );
314 // Non-existent attributes return null, we normalize to undefined
315 return attr === null ? undefined : attr;
318 // elem is actually elem.style ... set the style
319 // Using attr for specific style information is now deprecated. Use style insead.
320 return jQuery.style( elem, name, value );