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 addClass: function( value ) {
16 if ( jQuery.isFunction(value) ) {
17 return this.each(function() {
18 jQuery(this).addClass( value.call(this) );
22 if ( value && typeof value === "string" ) {
23 var classNames = (value || "").split( rspace );
25 for ( var i = 0, l = this.length; i < l; i++ ) {
28 if ( elem.nodeType === 1 ) {
29 if ( !elem.className ) {
30 elem.className = value;
33 var className = " " + elem.className + " ";
34 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
35 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
36 elem.className += " " + classNames[c];
47 removeClass: function( value ) {
48 if ( jQuery.isFunction(value) ) {
49 return this.each(function() {
50 jQuery(this).removeClass( value.call(this) );
54 if ( (value && typeof value === "string") || value === undefined ) {
55 var classNames = (value || "").split(rspace);
57 for ( var i = 0, l = this.length; i < l; i++ ) {
60 if ( elem.nodeType === 1 && elem.className ) {
62 var className = (" " + elem.className + " ").replace(rclass, " ");
63 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
64 className = className.replace(" " + classNames[c] + " ", " ");
66 elem.className = className.substring(1, className.length - 1);
78 hasClass: function( selector ) {
79 var className = " " + selector + " ";
80 for ( var i = 0, l = this.length; i < l; i++ ) {
81 if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
89 val: function( value ) {
90 if ( value === undefined ) {
94 if ( jQuery.nodeName( elem, "option" ) ) {
95 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
98 // We need to handle select boxes special
99 if ( jQuery.nodeName( elem, "select" ) ) {
100 var index = elem.selectedIndex,
102 options = elem.options,
103 one = elem.type === "select-one";
105 // Nothing was selected
110 // Loop through all the selected options
111 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
112 var option = options[ i ];
114 if ( option.selected ) {
115 // Get the specifc value for the option
116 value = jQuery(option).val();
118 // We don't need an array for one selects
123 // Multi-Selects return an array
124 values.push( value );
131 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
132 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
133 return elem.getAttribute("value") === null ? "on" : elem.value;
137 // Everything else, we just grab the value
138 return (elem.value || "").replace(rreturn, "");
145 // Typecast once if the value is a number
146 if ( typeof value === "number" ) {
152 return this.each(function() {
153 if ( jQuery.isFunction(value) ) {
154 val = value.call(this);
156 // Typecast each time if the value is a Function and the appended
157 // value is therefore different each time.
158 if ( typeof val === "number" ) {
163 if ( this.nodeType !== 1 ) {
167 if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
168 this.checked = jQuery.inArray( jQuery(this).val(), val ) >= 0;
170 } else if ( jQuery.nodeName( this, "select" ) ) {
171 var values = jQuery.makeArray(val);
173 jQuery( "option", this ).each(function() {
174 this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
177 if ( !values.length ) {
178 this.selectedIndex = -1;
189 removeAttr: function( name ) {
190 jQuery.attr( this, name, "" );
191 if ( this.nodeType === 1 ) {
192 this.removeAttribute( name );
196 toggleClass: function( classNames, state ) {
197 var type = typeof classNames;
199 if ( type === "string" ) {
200 // toggle individual class names
201 var isBool = typeof state === "boolean", className, i = 0,
202 classNames = classNames.split( rspace );
204 while ( (className = classNames[ i++ ]) ) {
205 // check each className given, space seperated list
206 state = isBool ? state : !jQuery(this).hasClass( className );
207 jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
210 } else if ( type === "undefined" || type === "boolean" ) {
211 if ( this.className ) {
212 // store className if set
213 jQuery.data( this, "__className__", this.className );
216 // toggle whole className
217 this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
220 }, function( name, fn ) {
221 jQuery.fn[ name ] = function( val, state ) {
222 if ( jQuery.isFunction( val ) ) {
223 return this.each(function() {
224 jQuery(this)[ name ]( val.call(this), state );
228 return this.each( fn, arguments );
244 attr: function( elem, name, value, pass ) {
245 // don't set attributes on text and comment nodes
246 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
250 if ( pass && name in jQuery.attrFn ) {
251 return jQuery(elem)[name](value);
254 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
255 // Whether we are setting (or getting)
256 set = value !== undefined;
258 // Try to normalize/fix the name
259 name = notxml && jQuery.props[ name ] || name;
261 // Only do all the following if this is a node (faster for style)
262 if ( elem.nodeType === 1 ) {
263 // These attributes require special treatment
264 var special = rspecialurl.test( name );
266 // Safari mis-reports the default selected property of an option
267 // Accessing the parent's selectedIndex property fixes it
268 if ( name === "selected" && !jQuery.support.optSelected ) {
269 var parent = elem.parentNode;
271 parent.selectedIndex;
273 // Make sure that it also works with optgroups, see #5701
274 if ( parent.parentNode ) {
275 parent.parentNode.selectedIndex;
280 // If applicable, access the attribute via the DOM 0 way
281 if ( name in elem && notxml && !special ) {
283 // We can't allow the type property to be changed (since it causes problems in IE)
284 if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
285 throw "type property can't be changed";
288 elem[ name ] = value;
291 // browsers index elements by id/name on forms, give priority to attributes.
292 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
293 return elem.getAttributeNode( name ).nodeValue;
296 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
297 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
298 if ( name === "tabIndex" ) {
299 var attributeNode = elem.getAttributeNode( "tabIndex" );
301 return attributeNode && attributeNode.specified ?
302 attributeNode.value :
303 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
311 if ( !jQuery.support.style && notxml && name === "style" ) {
313 elem.style.cssText = "" + value;
316 return elem.style.cssText;
320 // convert the value to a string (all browsers do this but IE) see #1070
321 elem.setAttribute( name, "" + value );
324 var attr = !jQuery.support.hrefNormalized && notxml && special ?
325 // Some attributes require a special call on IE
326 elem.getAttribute( name, 2 ) :
327 elem.getAttribute( name );
329 // Non-existent attributes return null, we normalize to undefined
330 return attr === null ? undefined : attr;
333 // elem is actually elem.style ... set the style
334 // Using attr for specific style information is now deprecated. Use style insead.
335 return jQuery.style( elem, name, value );