2 attr: function( name, value ) {
3 var elem, options, isFunction = jQuery.isFunction(value);
5 if ( typeof name === "string" ) { // A single attribute
6 if ( value === undefined ) { // Query it on first element
8 jQuery.attr( this[0], name ) :
10 } else { // Set it on all elements
11 for ( var i = 0, l = this.length; i < l; i++ ) {
14 value = value.call(elem,i);
15 jQuery.attr( elem, name, value );
18 } else { // Multiple attributes to set on all
20 for ( var i = 0, l = this.length; i < l; i++ ) {
22 for ( name in options ) {
23 value = options[name];
24 if ( jQuery.isFunction(value) )
25 value = value.call(elem,i);
26 jQuery.attr( elem, name, value );
34 addClass: function( value ) {
35 if ( value && typeof value === "string" ) {
36 var classNames = (value || "").split(/\s+/);
38 for ( var i = 0, l = this.length; i < l; i++ ) {
41 if ( elem.nodeType === 1 ) {
42 if ( !elem.className ) {
43 elem.className = value;
45 var className = " " + elem.className + " ";
46 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
47 if ( className.indexOf( " " + classNames[c] + " " ) < 0 ) {
48 elem.className += " " + classNames[c];
59 removeClass: function( value ) {
60 if ( (value && typeof value === "string") || value === undefined ) {
61 var classNames = (value || "").split(/\s+/);
63 for ( var i = 0, l = this.length; i < l; i++ ) {
66 if ( elem.nodeType === 1 && elem.className ) {
68 var className = " " + elem.className + " ";
69 for ( var c = 0, cl = classNames.length; c < cl; c++ ) {
70 className = className.replace(" " + classNames[c] + " ", " ");
72 elem.className = className.substring(1, className.length - 1);
83 hasClass: function( selector ) {
84 var className = " " + selector + " ";
85 for ( var i = 0, l = this.length; i < l; i++ ) {
86 if ( (" " + this[i].className + " ").indexOf( className ) > -1 ) {
94 val: function( value ) {
95 if ( value === undefined ) {
99 if( jQuery.nodeName( elem, 'option' ) )
100 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
102 // We need to handle select boxes special
103 if ( jQuery.nodeName( elem, "select" ) ) {
104 var index = elem.selectedIndex,
106 options = elem.options,
107 one = elem.type == "select-one";
109 // Nothing was selected
113 // Loop through all the selected options
114 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
115 var option = options[ i ];
117 if ( option.selected ) {
118 // Get the specifc value for the option
119 value = jQuery(option).val();
121 // We don't need an array for one selects
125 // Multi-Selects return an array
126 values.push( value );
133 // Everything else, we just grab the value
134 return (elem.value || "").replace(/\r/g, "");
141 // Typecast once if the value is a number
142 if ( typeof value === "number" )
147 return this.each(function(){
148 if(jQuery.isFunction(value)) {
149 val = value.call(this);
150 // Typecast each time if the value is a Function and the appended
151 // value is therefore different each time.
152 if( typeof val === "number" ) val += '';
155 if ( this.nodeType != 1 )
158 if ( jQuery.isArray(val) && /radio|checkbox/.test( this.type ) )
159 this.checked = jQuery.inArray(this.value || this.name, val) >= 0;
161 else if ( jQuery.nodeName( this, "select" ) ) {
162 var values = jQuery.makeArray(val);
164 jQuery( "option", this ).each(function(){
165 this.selected = jQuery.inArray( this.value || this.text, values ) >= 0;
168 if ( !values.length )
169 this.selectedIndex = -1;
178 removeAttr: function( name ) {
179 jQuery.attr( this, name, "" );
180 if (this.nodeType == 1)
181 this.removeAttribute( name );
184 toggleClass: function( classNames, state ) {
185 var type = typeof classNames;
186 if ( type === "string" ) {
187 // toggle individual class names
188 var isBool = typeof state === "boolean", className, i = 0,
189 classNames = classNames.split( /\s+/ );
190 while ( (className = classNames[ i++ ]) ) {
191 // check each className given, space seperated list
192 state = isBool ? state : !jQuery(this).hasClass( className );
193 jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
195 } else if ( type === "undefined" || type === "boolean" ) {
196 if ( this.className ) {
197 // store className if set
198 jQuery.data( this, "__className__", this.className );
200 // toggle whole className
201 this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
204 }, function(name, fn){
205 jQuery.fn[ name ] = function(){
206 return this.each( fn, arguments );
211 attr: function( elem, name, value ) {
212 // don't set attributes on text and comment nodes
213 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
216 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
217 // Whether we are setting (or getting)
218 set = value !== undefined;
220 // Try to normalize/fix the name
221 name = notxml && jQuery.props[ name ] || name;
223 // Only do all the following if this is a node (faster for style)
224 if ( elem.nodeType === 1 ) {
226 // These attributes require special treatment
227 var special = /href|src|style/.test( name );
229 // Safari mis-reports the default selected property of a hidden option
230 // Accessing the parent's selectedIndex property fixes it
231 if ( name == "selected" && elem.parentNode )
232 elem.parentNode.selectedIndex;
234 // If applicable, access the attribute via the DOM 0 way
235 if ( name in elem && notxml && !special ) {
237 // We can't allow the type property to be changed (since it causes problems in IE)
238 if ( name == "type" && /(button|input)/i.test(elem.nodeName) && elem.parentNode )
239 throw "type property can't be changed";
241 elem[ name ] = value;
244 // browsers index elements by id/name on forms, give priority to attributes.
245 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
246 return elem.getAttributeNode( name ).nodeValue;
248 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
249 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
250 if ( name == "tabIndex" ) {
251 var attributeNode = elem.getAttributeNode( "tabIndex" );
252 return attributeNode && attributeNode.specified
253 ? attributeNode.value
254 : /(button|input|object|select|textarea)/i.test(elem.nodeName)
256 : /^(a|area)$/i.test(elem.nodeName) && elem.href
264 if ( !jQuery.support.style && notxml && name == "style" ) {
266 elem.style.cssText = "" + value;
268 return elem.style.cssText;
272 // convert the value to a string (all browsers do this but IE) see #1070
273 elem.setAttribute( name, "" + value );
275 var attr = !jQuery.support.hrefNormalized && notxml && special
276 // Some attributes require a special call on IE
277 ? elem.getAttribute( name, 2 )
278 : elem.getAttribute( name );
280 // Non-existent attributes return null, we normalize to undefined
281 return attr === null ? undefined : attr;
284 // elem is actually elem.style ... set the style
285 // Using attr for specific style information is now deprecated. Use style insead.
286 return jQuery.style(elem, name, value);