2 attr: function( name, value ) {
3 return access(this, name, value, true, jQuery.attr);
6 addClass: function( value ) {
7 if ( value && typeof value === "string" ) {
8 var classNames = (value || "").split(/\s+/);
10 for ( var i = 0, l = this.length; i < l; i++ ) {
13 if ( elem.nodeType === 1 ) {
14 if ( !elem.className ) {
15 elem.className = value;
17 var className = " " + elem.className + " ";
18 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
19 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
20 elem.className += " " + classNames[c];
31 removeClass: function( value ) {
32 if ( (value && typeof value === "string") || value === undefined ) {
33 var classNames = (value || "").split(/\s+/);
35 for ( var i = 0, l = this.length; i < l; i++ ) {
38 if ( elem.nodeType === 1 && elem.className ) {
40 var className = " " + elem.className + " ";
41 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
42 className = className.replace(" " + classNames[c] + " ", " ");
44 elem.className = className.substring(1, className.length - 1);
55 hasClass: function( selector ) {
56 var className = " " + selector + " ";
57 for ( var i = 0, l = this.length; i < l; i++ ) {
58 if ( (" " + this[i].className + " ").indexOf( className ) > -1 ) {
66 val: function( value ) {
67 if ( value === undefined ) {
71 if( jQuery.nodeName( elem, 'option' ) ) {
72 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
74 // We need to handle select boxes special
75 if ( jQuery.nodeName( elem, "select" ) ) {
76 var index = elem.selectedIndex,
78 options = elem.options,
79 one = elem.type == "select-one";
81 // Nothing was selected
85 // Loop through all the selected options
86 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
87 var option = options[ i ];
89 if ( option.selected ) {
90 // Get the specifc value for the option
91 value = jQuery(option).val();
93 // We don't need an array for one selects
97 // Multi-Selects return an array
105 // Everything else, we just grab the value
106 return (elem.value || "").replace(/\r/g, "");
113 // Typecast once if the value is a number
114 if ( typeof value === "number" ) {
119 return this.each(function(){
120 if(jQuery.isFunction(value)) {
121 val = value.call(this);
122 // Typecast each time if the value is a Function and the appended
123 // value is therefore different each time.
124 if( typeof val === "number" ) {
129 if ( this.nodeType != 1 ) {
132 if ( jQuery.isArray(val) && /radio|checkbox/.test( this.type ) ) {
133 this.checked = jQuery.inArray(this.value || this.name, val) >= 0;
135 else if ( jQuery.nodeName( this, "select" ) ) {
136 var values = jQuery.makeArray(val);
138 jQuery( "option", this ).each(function(){
139 this.selected = jQuery.inArray( this.value || this.text, values ) >= 0;
142 if ( !values.length ) {
143 this.selectedIndex = -1;
153 removeAttr: function( name ) {
154 jQuery.attr( this, name, "" );
155 if (this.nodeType == 1) {
156 this.removeAttribute( name );
160 toggleClass: function( classNames, state ) {
161 var type = typeof classNames;
162 if ( type === "string" ) {
163 // toggle individual class names
164 var isBool = typeof state === "boolean", className, i = 0,
165 classNames = classNames.split( /\s+/ );
166 while ( (className = classNames[ i++ ]) ) {
167 // check each className given, space seperated list
168 state = isBool ? state : !jQuery(this).hasClass( className );
169 jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
171 } else if ( type === "undefined" || type === "boolean" ) {
172 if ( this.className ) {
173 // store className if set
174 jQuery.data( this, "__className__", this.className );
176 // toggle whole className
177 this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
180 }, function(name, fn){
181 jQuery.fn[ name ] = function(){
182 return this.each( fn, arguments );
187 attr: function( elem, name, value ) {
188 // don't set attributes on text and comment nodes
189 if (!elem || elem.nodeType == 3 || elem.nodeType == 8) {
192 if ( name in jQuery.fn && name !== "attr" ) {
193 return jQuery(elem)[name](value);
196 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
197 // Whether we are setting (or getting)
198 set = value !== undefined;
200 // Try to normalize/fix the name
201 name = notxml && jQuery.props[ name ] || name;
203 // Only do all the following if this is a node (faster for style)
204 if ( elem.nodeType === 1 ) {
206 // These attributes require special treatment
207 var special = /href|src|style/.test( name );
209 // Safari mis-reports the default selected property of a hidden option
210 // Accessing the parent's selectedIndex property fixes it
211 if ( name == "selected" && elem.parentNode ) {
212 elem.parentNode.selectedIndex;
214 // If applicable, access the attribute via the DOM 0 way
215 if ( name in elem && notxml && !special ) {
217 // We can't allow the type property to be changed (since it causes problems in IE)
218 if ( name == "type" && /(button|input)/i.test(elem.nodeName) && elem.parentNode ) {
219 throw "type property can't be changed";
221 elem[ name ] = value;
224 // browsers index elements by id/name on forms, give priority to attributes.
225 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) {
226 return elem.getAttributeNode( name ).nodeValue;
228 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
229 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
230 if ( name == "tabIndex" ) {
231 var attributeNode = elem.getAttributeNode( "tabIndex" );
232 return attributeNode && attributeNode.specified
233 ? attributeNode.value
234 : /(button|input|object|select|textarea)/i.test(elem.nodeName)
236 : /^(a|area)$/i.test(elem.nodeName) && elem.href
244 if ( !jQuery.support.style && notxml && name == "style" ) {
246 elem.style.cssText = "" + value;
248 return elem.style.cssText;
252 // convert the value to a string (all browsers do this but IE) see #1070
253 elem.setAttribute( name, "" + value );
255 var attr = !jQuery.support.hrefNormalized && notxml && special
256 // Some attributes require a special call on IE
257 ? elem.getAttribute( name, 2 )
258 : elem.getAttribute( name );
260 // Non-existent attributes return null, we normalize to undefined
261 return attr === null ? undefined : attr;
264 // elem is actually elem.style ... set the style
265 // Using attr for specific style information is now deprecated. Use style insead.
266 return jQuery.style(elem, name, value);