1 var rclass = /[\n\t]/g;
4 attr: function( name, value ) {
5 return access(this, name, value, true, jQuery.attr);
8 addClass: function( value ) {
9 if(jQuery.isFunction(value)) {
10 return this.each(function() {
11 jQuery(this).addClass( value.call(this) );
15 if ( value && typeof value === "string" ) {
16 var classNames = (value || "").split(/\s+/);
18 for ( var i = 0, l = this.length; i < l; i++ ) {
21 if ( elem.nodeType === 1 ) {
22 if ( !elem.className ) {
23 elem.className = value;
25 var className = " " + elem.className + " ";
26 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
27 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
28 elem.className += " " + classNames[c];
39 removeClass: function( value ) {
40 if(jQuery.isFunction(value)) {
41 return this.each(function() {
42 jQuery(this).removeClass( value.call(this) );
46 if ( (value && typeof value === "string") || value === undefined ) {
47 var classNames = (value || "").split(/\s+/);
49 for ( var i = 0, l = this.length; i < l; i++ ) {
52 if ( elem.nodeType === 1 && elem.className ) {
54 var className = (" " + elem.className + " ").replace(rclass, " ");
55 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
56 className = className.replace(" " + classNames[c] + " ", " ");
58 elem.className = className.substring(1, className.length - 1);
69 hasClass: function( selector ) {
70 var className = " " + selector + " ";
71 for ( var i = 0, l = this.length; i < l; i++ ) {
72 if ( (" " + this[i].className + " ").replace(rclass, " ").indexOf( className ) > -1 ) {
80 val: function( value ) {
81 if ( value === undefined ) {
85 if( jQuery.nodeName( elem, 'option' ) ) {
86 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
88 // We need to handle select boxes special
89 if ( jQuery.nodeName( elem, "select" ) ) {
90 var index = elem.selectedIndex,
92 options = elem.options,
93 one = elem.type == "select-one";
95 // Nothing was selected
99 // Loop through all the selected options
100 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
101 var option = options[ i ];
103 if ( option.selected ) {
104 // Get the specifc value for the option
105 value = jQuery(option).val();
107 // We don't need an array for one selects
111 // Multi-Selects return an array
112 values.push( value );
119 // Everything else, we just grab the value
120 return (elem.value || "").replace(/\r/g, "");
127 // Typecast once if the value is a number
128 if ( typeof value === "number" ) {
133 return this.each(function(){
134 if(jQuery.isFunction(value)) {
135 val = value.call(this);
136 // Typecast each time if the value is a Function and the appended
137 // value is therefore different each time.
138 if( typeof val === "number" ) {
143 if ( this.nodeType != 1 ) {
146 if ( jQuery.isArray(val) && /radio|checkbox/.test( this.type ) ) {
147 this.checked = jQuery.inArray(this.value || this.name, val) >= 0;
149 else if ( jQuery.nodeName( this, "select" ) ) {
150 var values = jQuery.makeArray(val);
152 jQuery( "option", this ).each(function(){
153 this.selected = jQuery.inArray( this.value || this.text, values ) >= 0;
156 if ( !values.length ) {
157 this.selectedIndex = -1;
167 removeAttr: function( name ) {
168 jQuery.attr( this, name, "" );
169 if ( this.nodeType === 1 ) {
170 this.removeAttribute( name );
174 toggleClass: function( classNames, state ) {
175 var type = typeof classNames;
176 if ( type === "string" ) {
177 // toggle individual class names
178 var isBool = typeof state === "boolean", className, i = 0,
179 classNames = classNames.split( /\s+/ );
180 while ( (className = classNames[ i++ ]) ) {
181 // check each className given, space seperated list
182 state = isBool ? state : !jQuery(this).hasClass( className );
183 jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
185 } else if ( type === "undefined" || type === "boolean" ) {
186 if ( this.className ) {
187 // store className if set
188 jQuery.data( this, "__className__", this.className );
190 // toggle whole className
191 this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
194 }, function(name, fn){
195 jQuery.fn[ name ] = function(val, state){
196 if( jQuery.isFunction( val ) ) {
197 return this.each(function() { jQuery(this)[ name ]( val.call(this), state ); });
200 return this.each( fn, arguments );
216 attr: function( elem, name, value ) {
217 // don't set attributes on text and comment nodes
218 if (!elem || elem.nodeType == 3 || elem.nodeType == 8) {
222 if ( name in jQuery.attrFn && value !== undefined ) {
223 return jQuery(elem)[name](value);
226 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
227 // Whether we are setting (or getting)
228 set = value !== undefined;
230 // Try to normalize/fix the name
231 name = notxml && jQuery.props[ name ] || name;
233 // Only do all the following if this is a node (faster for style)
234 if ( elem.nodeType === 1 ) {
236 // These attributes require special treatment
237 var special = /href|src|style/.test( name );
239 // Safari mis-reports the default selected property of a hidden option
240 // Accessing the parent's selectedIndex property fixes it
241 if ( name == "selected" && elem.parentNode ) {
242 elem.parentNode.selectedIndex;
244 // If applicable, access the attribute via the DOM 0 way
245 if ( name in elem && notxml && !special ) {
247 // We can't allow the type property to be changed (since it causes problems in IE)
248 if ( name == "type" && /(button|input)/i.test(elem.nodeName) && elem.parentNode ) {
249 throw "type property can't be changed";
251 // browsers index elements by id/name on forms, give priority to attributes.
252 if( jQuery.nodeName( elem, "form" ) ) {
253 // convert the value to a string (all browsers do this but IE) see #1070
254 elem.setAttribute( name, "" + value );
256 elem[ name ] = value;
260 // browsers index elements by id/name on forms, give priority to attributes.
261 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
262 return elem.getAttributeNode( name ).nodeValue;
264 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
265 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
266 if ( name == "tabIndex" ) {
267 var attributeNode = elem.getAttributeNode( "tabIndex" );
268 return attributeNode && attributeNode.specified
269 ? attributeNode.value
270 : /(button|input|object|select|textarea)/i.test(elem.nodeName)
272 : /^(a|area)$/i.test(elem.nodeName) && elem.href
280 if ( !jQuery.support.style && notxml && name == "style" ) {
282 elem.style.cssText = "" + value;
284 return elem.style.cssText;
288 // convert the value to a string (all browsers do this but IE) see #1070
289 elem.setAttribute( name, "" + value );
291 var attr = !jQuery.support.hrefNormalized && notxml && special
292 // Some attributes require a special call on IE
293 ? elem.getAttribute( name, 2 )
294 : elem.getAttribute( name );
296 // Non-existent attributes return null, we normalize to undefined
297 return attr === null ? undefined : attr;
300 // elem is actually elem.style ... set the style
301 // Using attr for specific style information is now deprecated. Use style insead.
302 return jQuery.style(elem, name, value);