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" ) val += '';
127 if ( this.nodeType != 1 )
130 if ( jQuery.isArray(val) && /radio|checkbox/.test( this.type ) )
131 this.checked = jQuery.inArray(this.value || this.name, val) >= 0;
133 else if ( jQuery.nodeName( this, "select" ) ) {
134 var values = jQuery.makeArray(val);
136 jQuery( "option", this ).each(function(){
137 this.selected = jQuery.inArray( this.value || this.text, values ) >= 0;
140 if ( !values.length )
141 this.selectedIndex = -1;
150 removeAttr: function( name ) {
151 jQuery.attr( this, name, "" );
152 if (this.nodeType == 1)
153 this.removeAttribute( name );
156 toggleClass: function( classNames, state ) {
157 var type = typeof classNames;
158 if ( type === "string" ) {
159 // toggle individual class names
160 var isBool = typeof state === "boolean", className, i = 0,
161 classNames = classNames.split( /\s+/ );
162 while ( (className = classNames[ i++ ]) ) {
163 // check each className given, space seperated list
164 state = isBool ? state : !jQuery(this).hasClass( className );
165 jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
167 } else if ( type === "undefined" || type === "boolean" ) {
168 if ( this.className ) {
169 // store className if set
170 jQuery.data( this, "__className__", this.className );
172 // toggle whole className
173 this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
176 }, function(name, fn){
177 jQuery.fn[ name ] = function(){
178 return this.each( fn, arguments );
183 attr: function( elem, name, value ) {
184 // don't set attributes on text and comment nodes
185 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
188 if ( name in jQuery.fn && name !== "attr" ) {
189 return jQuery(elem)[name](value);
192 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
193 // Whether we are setting (or getting)
194 set = value !== undefined;
196 // Try to normalize/fix the name
197 name = notxml && jQuery.props[ name ] || name;
199 // Only do all the following if this is a node (faster for style)
200 if ( elem.nodeType === 1 ) {
202 // These attributes require special treatment
203 var special = /href|src|style/.test( name );
205 // Safari mis-reports the default selected property of a hidden option
206 // Accessing the parent's selectedIndex property fixes it
207 if ( name == "selected" && elem.parentNode )
208 elem.parentNode.selectedIndex;
210 // If applicable, access the attribute via the DOM 0 way
211 if ( name in elem && notxml && !special ) {
213 // We can't allow the type property to be changed (since it causes problems in IE)
214 if ( name == "type" && /(button|input)/i.test(elem.nodeName) && elem.parentNode )
215 throw "type property can't be changed";
217 elem[ name ] = value;
220 // browsers index elements by id/name on forms, give priority to attributes.
221 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
222 return elem.getAttributeNode( name ).nodeValue;
224 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
225 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
226 if ( name == "tabIndex" ) {
227 var attributeNode = elem.getAttributeNode( "tabIndex" );
228 return attributeNode && attributeNode.specified
229 ? attributeNode.value
230 : /(button|input|object|select|textarea)/i.test(elem.nodeName)
232 : /^(a|area)$/i.test(elem.nodeName) && elem.href
240 if ( !jQuery.support.style && notxml && name == "style" ) {
242 elem.style.cssText = "" + value;
244 return elem.style.cssText;
248 // convert the value to a string (all browsers do this but IE) see #1070
249 elem.setAttribute( name, "" + value );
251 var attr = !jQuery.support.hrefNormalized && notxml && special
252 // Some attributes require a special call on IE
253 ? elem.getAttribute( name, 2 )
254 : elem.getAttribute( name );
256 // Non-existent attributes return null, we normalize to undefined
257 return attr === null ? undefined : attr;
260 // elem is actually elem.style ... set the style
261 // Using attr for specific style information is now deprecated. Use style insead.
262 return jQuery.style(elem, name, value);