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 jQuery.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 + " ", setClass = elem.className;
44 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
45 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
46 setClass += " " + classNames[c];
49 elem.className = jQuery.trim( setClass );
58 removeClass: function( value ) {
59 if ( jQuery.isFunction(value) ) {
60 return this.each(function(i) {
61 var self = jQuery(this);
62 self.removeClass( value.call(this, i, self.attr("class")) );
66 if ( (value && typeof value === "string") || value === undefined ) {
67 var classNames = (value || "").split(rspace);
69 for ( var i = 0, l = this.length; i < l; i++ ) {
72 if ( elem.nodeType === 1 && elem.className ) {
74 var className = (" " + elem.className + " ").replace(rclass, " ");
75 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
76 className = className.replace(" " + classNames[c] + " ", " ");
78 elem.className = jQuery.trim( className );
90 toggleClass: function( value, stateVal ) {
91 var type = typeof value, isBool = typeof stateVal === "boolean";
93 if ( jQuery.isFunction( value ) ) {
94 return this.each(function(i) {
95 var self = jQuery(this);
96 self.toggleClass( value.call(this, i, self.attr("class"), stateVal), stateVal );
100 return this.each(function() {
101 if ( type === "string" ) {
102 // toggle individual class names
103 var className, i = 0, self = jQuery(this),
105 classNames = value.split( rspace );
107 while ( (className = classNames[ i++ ]) ) {
108 // check each className given, space seperated list
109 state = isBool ? state : !self.hasClass( className );
110 self[ state ? "addClass" : "removeClass" ]( className );
113 } else if ( type === "undefined" || type === "boolean" ) {
114 if ( this.className ) {
115 // store className if set
116 jQuery.data( this, "__className__", this.className );
119 // toggle whole className
120 this.className = this.className || value === false ? "" : jQuery.data( this, "__className__" ) || "";
125 hasClass: function( selector ) {
126 var className = " " + selector + " ";
127 for ( var i = 0, l = this.length; i < l; i++ ) {
128 if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
136 val: function( value ) {
137 if ( value === undefined ) {
141 if ( jQuery.nodeName( elem, "option" ) ) {
142 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
145 // We need to handle select boxes special
146 if ( jQuery.nodeName( elem, "select" ) ) {
147 var index = elem.selectedIndex,
149 options = elem.options,
150 one = elem.type === "select-one";
152 // Nothing was selected
157 // Loop through all the selected options
158 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
159 var option = options[ i ];
161 if ( option.selected ) {
162 // Get the specifc value for the option
163 value = jQuery(option).val();
165 // We don't need an array for one selects
170 // Multi-Selects return an array
171 values.push( value );
178 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
179 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
180 return elem.getAttribute("value") === null ? "on" : elem.value;
184 // Everything else, we just grab the value
185 return (elem.value || "").replace(rreturn, "");
192 var isFunction = jQuery.isFunction(value);
194 return this.each(function(i) {
195 var self = jQuery(this), val = value;
197 if ( this.nodeType !== 1 ) {
202 val = value.call(this, i, self.val());
205 // Typecast each time if the value is a Function and the appended
206 // value is therefore different each time.
207 if ( typeof val === "number" ) {
211 if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
212 this.checked = jQuery.inArray( self.val(), val ) >= 0;
214 } else if ( jQuery.nodeName( this, "select" ) ) {
215 var values = jQuery.makeArray(val);
217 jQuery( "option", this ).each(function() {
218 this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
221 if ( !values.length ) {
222 this.selectedIndex = -1;
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 jQuery.error( "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 instead.
335 return jQuery.style( elem, name, value );