2 attr: function( name, value ) {
3 return access(this, name, value, true, jQuery.attr);
6 addClass: function( value ) {
7 if(jQuery.isFunction(value)) {
8 return this.each(function() {
9 jQuery(this).addClass( value.call(this) );
13 if ( value && typeof value === "string" ) {
14 var classNames = (value || "").split(/\s+/);
16 for ( var i = 0, l = this.length; i < l; i++ ) {
19 if ( elem.nodeType === 1 ) {
20 if ( !elem.className ) {
21 elem.className = value;
23 var className = " " + elem.className + " ";
24 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
25 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
26 elem.className += " " + classNames[c];
37 removeClass: function( value ) {
38 if(jQuery.isFunction(value)) {
39 return this.each(function() {
40 jQuery(this).removeClass( value.call(this) );
44 if ( (value && typeof value === "string") || value === undefined ) {
45 var classNames = (value || "").split(/\s+/);
47 for ( var i = 0, l = this.length; i < l; i++ ) {
50 if ( elem.nodeType === 1 && elem.className ) {
52 var className = " " + elem.className + " ";
53 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
54 className = className.replace(" " + classNames[c] + " ", " ");
56 elem.className = className.substring(1, className.length - 1);
67 hasClass: function( selector ) {
68 var className = " " + selector + " ";
69 for ( var i = 0, l = this.length; i < l; i++ ) {
70 if ( (" " + this[i].className + " ").indexOf( className ) > -1 ) {
78 val: function( value ) {
79 if ( value === undefined ) {
83 if( jQuery.nodeName( elem, 'option' ) ) {
84 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
86 // We need to handle select boxes special
87 if ( jQuery.nodeName( elem, "select" ) ) {
88 var index = elem.selectedIndex,
90 options = elem.options,
91 one = elem.type == "select-one";
93 // Nothing was selected
97 // Loop through all the selected options
98 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
99 var option = options[ i ];
101 if ( option.selected ) {
102 // Get the specifc value for the option
103 value = jQuery(option).val();
105 // We don't need an array for one selects
109 // Multi-Selects return an array
110 values.push( value );
117 // Everything else, we just grab the value
118 return (elem.value || "").replace(/\r/g, "");
125 // Typecast once if the value is a number
126 if ( typeof value === "number" ) {
131 return this.each(function(){
132 if(jQuery.isFunction(value)) {
133 val = value.call(this);
134 // Typecast each time if the value is a Function and the appended
135 // value is therefore different each time.
136 if( typeof val === "number" ) {
141 if ( this.nodeType != 1 ) {
144 if ( jQuery.isArray(val) && /radio|checkbox/.test( this.type ) ) {
145 this.checked = jQuery.inArray(this.value || this.name, val) >= 0;
147 else if ( jQuery.nodeName( this, "select" ) ) {
148 var values = jQuery.makeArray(val);
150 jQuery( "option", this ).each(function(){
151 this.selected = jQuery.inArray( this.value || this.text, values ) >= 0;
154 if ( !values.length ) {
155 this.selectedIndex = -1;
165 removeAttr: function( name ) {
166 jQuery.attr( this, name, "" );
167 if (this.nodeType == 1) {
168 this.removeAttribute( name );
172 toggleClass: function( classNames, state ) {
173 if( jQuery.isFunction(classNames) ) {
174 return this.each(function() {
176 jQuery(this).toggleClass( classNames.call(this), state );
180 var type = typeof classNames;
181 if ( type === "string" ) {
182 // toggle individual class names
183 var isBool = typeof state === "boolean", className, i = 0,
184 classNames = classNames.split( /\s+/ );
185 while ( (className = classNames[ i++ ]) ) {
186 // check each className given, space seperated list
187 state = isBool ? state : !jQuery(this).hasClass( className );
188 jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
190 } else if ( type === "undefined" || type === "boolean" ) {
191 if ( this.className ) {
192 // store className if set
193 jQuery.data( this, "__className__", this.className );
195 // toggle whole className
196 this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
199 }, function(name, fn){
200 jQuery.fn[ name ] = function(val, state){
201 if( jQuery.isFunction( val ) ) {
202 return this.each(function() { jQuery(this)[ name ]( val.call(this), state ); });
205 return this.each( fn, arguments );
210 attr: function( elem, name, value ) {
211 // don't set attributes on text and comment nodes
212 if (!elem || elem.nodeType == 3 || elem.nodeType == 8) {
215 if ( name in jQuery.fn && name !== "attr" ) {
216 return jQuery(elem)[name](value);
219 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
220 // Whether we are setting (or getting)
221 set = value !== undefined;
223 // Try to normalize/fix the name
224 name = notxml && jQuery.props[ name ] || name;
226 // Only do all the following if this is a node (faster for style)
227 if ( elem.nodeType === 1 ) {
229 // These attributes require special treatment
230 var special = /href|src|style/.test( name );
232 // Safari mis-reports the default selected property of a hidden option
233 // Accessing the parent's selectedIndex property fixes it
234 if ( name == "selected" && elem.parentNode ) {
235 elem.parentNode.selectedIndex;
237 // If applicable, access the attribute via the DOM 0 way
238 if ( name in elem && notxml && !special ) {
240 // We can't allow the type property to be changed (since it causes problems in IE)
241 if ( name == "type" && /(button|input)/i.test(elem.nodeName) && elem.parentNode ) {
242 throw "type property can't be changed";
244 // browsers index elements by id/name on forms, give priority to attributes.
245 if( jQuery.nodeName( elem, "form" ) ) {
246 // convert the value to a string (all browsers do this but IE) see #1070
247 elem.setAttribute( name, "" + value );
249 elem[ name ] = value;
253 // browsers index elements by id/name on forms, give priority to attributes.
254 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
255 return elem.getAttributeNode( name ).nodeValue;
257 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
258 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
259 if ( name == "tabIndex" ) {
260 var attributeNode = elem.getAttributeNode( "tabIndex" );
261 return attributeNode && attributeNode.specified
262 ? attributeNode.value
263 : /(button|input|object|select|textarea)/i.test(elem.nodeName)
265 : /^(a|area)$/i.test(elem.nodeName) && elem.href
273 if ( !jQuery.support.style && notxml && name == "style" ) {
275 elem.style.cssText = "" + value;
277 return elem.style.cssText;
281 // convert the value to a string (all browsers do this but IE) see #1070
282 elem.setAttribute( name, "" + value );
284 var attr = !jQuery.support.hrefNormalized && notxml && special
285 // Some attributes require a special call on IE
286 ? elem.getAttribute( name, 2 )
287 : elem.getAttribute( name );
289 // Non-existent attributes return null, we normalize to undefined
290 return attr === null ? undefined : attr;
293 // elem is actually elem.style ... set the style
294 // Using attr for specific style information is now deprecated. Use style insead.
295 return jQuery.style(elem, name, value);