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 hasClass: function( selector ) {
35 return !!selector && this.is( "." + selector );
38 val: function( value ) {
39 if ( value === undefined ) {
43 if( jQuery.nodeName( elem, 'option' ) )
44 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
46 // We need to handle select boxes special
47 if ( jQuery.nodeName( elem, "select" ) ) {
48 var index = elem.selectedIndex,
50 options = elem.options,
51 one = elem.type == "select-one";
53 // Nothing was selected
57 // Loop through all the selected options
58 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
59 var option = options[ i ];
61 if ( option.selected ) {
62 // Get the specifc value for the option
63 value = jQuery(option).val();
65 // We don't need an array for one selects
69 // Multi-Selects return an array
77 // Everything else, we just grab the value
78 return (elem.value || "").replace(/\r/g, "");
85 if ( typeof value === "number" )
88 return this.each(function(){
89 if ( this.nodeType != 1 )
92 if ( jQuery.isArray(value) && /radio|checkbox/.test( this.type ) )
93 this.checked = (jQuery.inArray(this.value, value) >= 0 ||
94 jQuery.inArray(this.name, value) >= 0);
96 else if ( jQuery.nodeName( this, "select" ) ) {
97 var values = jQuery.makeArray(value);
99 jQuery( "option", this ).each(function(){
100 this.selected = (jQuery.inArray( this.value, values ) >= 0 ||
101 jQuery.inArray( this.text, values ) >= 0);
104 if ( !values.length )
105 this.selectedIndex = -1;
114 removeAttr: function( name ) {
115 jQuery.attr( this, name, "" );
116 if (this.nodeType == 1)
117 this.removeAttribute( name );
120 addClass: function( classNames ) {
121 jQuery.className.add( this, classNames );
124 removeClass: function( classNames ) {
125 jQuery.className.remove( this, classNames );
128 toggleClass: function( classNames, state ) {
129 var type = typeof classNames;
130 if ( type === "string" ) {
131 // toggle individual class names
132 var isBool = typeof state === "boolean", className, i = 0,
133 classNames = classNames.split( /\s+/ );
134 while ( (className = classNames[ i++ ]) ) {
135 // check each className given, space seperated list
136 state = isBool ? state : !jQuery.className.has( this, className );
137 jQuery.className[ state ? "add" : "remove" ]( this, className );
139 } else if ( type === "undefined" || type === "boolean" ) {
140 if ( this.className ) {
141 // store className if set
142 jQuery.data( this, "__className__", this.className );
144 // toggle whole className
145 this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
148 }, function(name, fn){
149 jQuery.fn[ name ] = function(){
150 return this.each( fn, arguments );
156 // internal only, use addClass("class")
157 add: function( elem, classNames ) {
158 jQuery.each((classNames || "").split(/\s+/), function(i, className){
159 if ( elem.nodeType == 1 && !jQuery.className.has( elem.className, className ) )
160 elem.className += (elem.className ? " " : "") + className;
164 // internal only, use removeClass("class")
165 remove: function( elem, classNames ) {
166 if (elem.nodeType == 1)
167 elem.className = classNames !== undefined ?
168 jQuery.grep(elem.className.split(/\s+/), function(className){
169 return !jQuery.className.has( classNames, className );
174 // internal only, use hasClass("class")
175 has: function( elem, className ) {
176 return elem && jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1;
180 attr: function( elem, name, value ) {
181 // don't set attributes on text and comment nodes
182 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
185 var notxml = !elem.tagName || !jQuery.isXMLDoc( elem ),
186 // Whether we are setting (or getting)
187 set = value !== undefined;
189 // Try to normalize/fix the name
190 name = notxml && jQuery.props[ name ] || name;
192 // Only do all the following if this is a node (faster for style)
193 if ( elem.tagName ) {
195 // These attributes require special treatment
196 var special = /href|src|style/.test( name );
198 // Safari mis-reports the default selected property of a hidden option
199 // Accessing the parent's selectedIndex property fixes it
200 if ( name == "selected" && elem.parentNode )
201 elem.parentNode.selectedIndex;
203 // If applicable, access the attribute via the DOM 0 way
204 if ( name in elem && notxml && !special ) {
206 // We can't allow the type property to be changed (since it causes problems in IE)
207 if ( name == "type" && elem.nodeName.match(/(button|input)/i) && elem.parentNode )
208 throw "type property can't be changed";
210 elem[ name ] = value;
213 // browsers index elements by id/name on forms, give priority to attributes.
214 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
215 return elem.getAttributeNode( name ).nodeValue;
217 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
218 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
219 if ( name == "tabIndex" ) {
220 var attributeNode = elem.getAttributeNode( "tabIndex" );
221 return attributeNode && attributeNode.specified
222 ? attributeNode.value
223 : elem.nodeName.match(/(button|input|object|select|textarea)/i)
225 : elem.nodeName.match(/^(a|area)$/i) && elem.href
233 if ( !jQuery.support.style && notxml && name == "style" ) {
235 elem.style.cssText = "" + value;
237 return elem.style.cssText;
241 // convert the value to a string (all browsers do this but IE) see #1070
242 elem.setAttribute( name, "" + value );
244 var attr = !jQuery.support.hrefNormalized && notxml && special
245 // Some attributes require a special call on IE
246 ? elem.getAttribute( name, 2 )
247 : elem.getAttribute( name );
249 // Non-existent attributes return null, we normalize to undefined
250 return attr === null ? undefined : attr;
253 // elem is actually elem.style ... set the style
254 // Using attr for specific style information is now deprecated. Use style insead.
255 return jQuery.style(elem, name, value);