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 // attributes.value is undefined in Blackberry 4.7 but
143 // uses .value. See #6932
144 var val = elem.attributes.value;
145 return !val || val.specified ? elem.value : elem.text;
148 // We need to handle select boxes special
149 if ( jQuery.nodeName( elem, "select" ) ) {
150 var index = elem.selectedIndex,
152 options = elem.options,
153 one = elem.type === "select-one";
155 // Nothing was selected
160 // Loop through all the selected options
161 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
162 var option = options[ i ];
164 if ( option.selected ) {
165 // Get the specifc value for the option
166 value = jQuery(option).val();
168 // We don't need an array for one selects
173 // Multi-Selects return an array
174 values.push( value );
181 // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
182 if ( rradiocheck.test( elem.type ) && !jQuery.support.checkOn ) {
183 return elem.getAttribute("value") === null ? "on" : elem.value;
187 // Everything else, we just grab the value
188 return (elem.value || "").replace(rreturn, "");
195 var isFunction = jQuery.isFunction(value);
197 return this.each(function(i) {
198 var self = jQuery(this), val = value;
200 if ( this.nodeType !== 1 ) {
205 val = value.call(this, i, self.val());
208 // Typecast each time if the value is a Function and the appended
209 // value is therefore different each time.
210 if ( typeof val === "number" ) {
214 if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
215 this.checked = jQuery.inArray( self.val(), val ) >= 0;
217 } else if ( jQuery.nodeName( this, "select" ) ) {
218 var values = jQuery.makeArray(val);
220 jQuery( "option", this ).each(function() {
221 this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
224 if ( !values.length ) {
225 this.selectedIndex = -1;
247 attr: function( elem, name, value, pass ) {
248 // don't set attributes on text and comment nodes
249 if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 ) {
253 if ( pass && name in jQuery.attrFn ) {
254 return jQuery(elem)[name](value);
257 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
258 // Whether we are setting (or getting)
259 set = value !== undefined;
261 // Try to normalize/fix the name
262 name = notxml && jQuery.props[ name ] || name;
264 // Only do all the following if this is a node (faster for style)
265 if ( elem.nodeType === 1 ) {
266 // These attributes require special treatment
267 var special = rspecialurl.test( name );
269 // Safari mis-reports the default selected property of an option
270 // Accessing the parent's selectedIndex property fixes it
271 if ( name === "selected" && !jQuery.support.optSelected ) {
272 var parent = elem.parentNode;
274 parent.selectedIndex;
276 // Make sure that it also works with optgroups, see #5701
277 if ( parent.parentNode ) {
278 parent.parentNode.selectedIndex;
283 // If applicable, access the attribute via the DOM 0 way
284 // 'in' checks fail in Blackberry 4.7 #6931
285 if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
287 // We can't allow the type property to be changed (since it causes problems in IE)
288 if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
289 jQuery.error( "type property can't be changed" );
292 elem[ name ] = value;
295 // browsers index elements by id/name on forms, give priority to attributes.
296 if ( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
297 return elem.getAttributeNode( name ).nodeValue;
300 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
301 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
302 if ( name === "tabIndex" ) {
303 var attributeNode = elem.getAttributeNode( "tabIndex" );
305 return attributeNode && attributeNode.specified ?
306 attributeNode.value :
307 rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
315 if ( !jQuery.support.style && notxml && name === "style" ) {
317 elem.style.cssText = "" + value;
320 return elem.style.cssText;
324 // convert the value to a string (all browsers do this but IE) see #1070
325 elem.setAttribute( name, "" + value );
328 // Ensure that missing attributes return undefined
329 // Blackberry 4.7 returns "" from getAttribute #6938
330 if ( !elem.attributes[ name ] && !elem.hasAttribute( name ) ) {
334 var attr = !jQuery.support.hrefNormalized && notxml && special ?
335 // Some attributes require a special call on IE
336 elem.getAttribute( name, 2 ) :
337 elem.getAttribute( name );
339 // Non-existent attributes return null, we normalize to undefined
340 return attr === null ? undefined : attr;
343 // elem is actually elem.style ... set the style
344 // Using attr for specific style information is now deprecated. Use style instead.
345 return jQuery.style( elem, name, value );