2 attr: function( name, value ) {
3 var options = name, isFunction = jQuery.isFunction( value );
5 if ( typeof name === "string" ) {
6 // Are we setting the attribute?
7 if ( value === undefined ) {
9 jQuery.attr( this[0], name ) :
12 // Convert name, value params to options hash format
15 options[ name ] = value;
19 // For each element...
20 for ( var i = 0, l = this.length; i < l; i++ ) {
23 // Set all the attributes
24 for ( var prop in options ) {
25 value = options[prop];
28 value = value.call( elem, i );
31 jQuery.attr( elem, prop, value );
38 hasClass: function( selector ) {
39 return !!selector && this.is( "." + selector );
42 val: function( value ) {
43 if ( value === undefined ) {
47 if( jQuery.nodeName( elem, 'option' ) )
48 return (elem.attributes.value || {}).specified ? elem.value : elem.text;
50 // We need to handle select boxes special
51 if ( jQuery.nodeName( elem, "select" ) ) {
52 var index = elem.selectedIndex,
54 options = elem.options,
55 one = elem.type == "select-one";
57 // Nothing was selected
61 // Loop through all the selected options
62 for ( var i = one ? index : 0, max = one ? index + 1 : options.length; i < max; i++ ) {
63 var option = options[ i ];
65 if ( option.selected ) {
66 // Get the specifc value for the option
67 value = jQuery(option).val();
69 // We don't need an array for one selects
73 // Multi-Selects return an array
81 // Everything else, we just grab the value
82 return (elem.value || "").replace(/\r/g, "");
89 if ( typeof value === "number" )
92 return this.each(function(){
93 if ( this.nodeType != 1 )
96 if ( jQuery.isArray(value) && /radio|checkbox/.test( this.type ) )
97 this.checked = (jQuery.inArray(this.value, value) >= 0 ||
98 jQuery.inArray(this.name, value) >= 0);
100 else if ( jQuery.nodeName( this, "select" ) ) {
101 var values = jQuery.makeArray(value);
103 jQuery( "option", this ).each(function(){
104 this.selected = (jQuery.inArray( this.value, values ) >= 0 ||
105 jQuery.inArray( this.text, values ) >= 0);
108 if ( !values.length )
109 this.selectedIndex = -1;
118 removeAttr: function( name ) {
119 jQuery.attr( this, name, "" );
120 if (this.nodeType == 1)
121 this.removeAttribute( name );
124 addClass: function( classNames ) {
125 jQuery.className.add( this, classNames );
128 removeClass: function( classNames ) {
129 jQuery.className.remove( this, classNames );
132 toggleClass: function( classNames, state ) {
133 if( typeof state !== "boolean" )
134 state = !jQuery.className.has( this, classNames );
135 jQuery.className[ state ? "add" : "remove" ]( this, classNames );
137 }, function(name, fn){
138 jQuery.fn[ name ] = function(){
139 return this.each( fn, arguments );
145 // internal only, use addClass("class")
146 add: function( elem, classNames ) {
147 jQuery.each((classNames || "").split(/\s+/), function(i, className){
148 if ( elem.nodeType == 1 && !jQuery.className.has( elem.className, className ) )
149 elem.className += (elem.className ? " " : "") + className;
153 // internal only, use removeClass("class")
154 remove: function( elem, classNames ) {
155 if (elem.nodeType == 1)
156 elem.className = classNames !== undefined ?
157 jQuery.grep(elem.className.split(/\s+/), function(className){
158 return !jQuery.className.has( classNames, className );
163 // internal only, use hasClass("class")
164 has: function( elem, className ) {
165 return elem && jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1;
169 attr: function( elem, name, value ) {
170 // don't set attributes on text and comment nodes
171 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
174 var notxml = !elem.tagName || !jQuery.isXMLDoc( elem ),
175 // Whether we are setting (or getting)
176 set = value !== undefined;
178 // Try to normalize/fix the name
179 name = notxml && jQuery.props[ name ] || name;
181 // Only do all the following if this is a node (faster for style)
182 if ( elem.tagName ) {
184 // These attributes require special treatment
185 var special = /href|src|style/.test( name );
187 // Safari mis-reports the default selected property of a hidden option
188 // Accessing the parent's selectedIndex property fixes it
189 if ( name == "selected" && elem.parentNode )
190 elem.parentNode.selectedIndex;
192 // If applicable, access the attribute via the DOM 0 way
193 if ( name in elem && notxml && !special ) {
195 // We can't allow the type property to be changed (since it causes problems in IE)
196 if ( name == "type" && elem.nodeName.match(/(button|input)/i) && elem.parentNode )
197 throw "type property can't be changed";
199 elem[ name ] = value;
202 // browsers index elements by id/name on forms, give priority to attributes.
203 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
204 return elem.getAttributeNode( name ).nodeValue;
206 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
207 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
208 if ( name == "tabIndex" ) {
209 var attributeNode = elem.getAttributeNode( "tabIndex" );
210 return attributeNode && attributeNode.specified
211 ? attributeNode.value
212 : elem.nodeName.match(/(button|input|object|select|textarea)/i)
214 : elem.nodeName.match(/^(a|area)$/i) && elem.href
222 if ( !jQuery.support.style && notxml && name == "style" ) {
224 elem.style.cssText = "" + value;
226 return elem.style.cssText;
230 // convert the value to a string (all browsers do this but IE) see #1070
231 elem.setAttribute( name, "" + value );
233 var attr = !jQuery.support.hrefNormalized && notxml && special
234 // Some attributes require a special call on IE
235 ? elem.getAttribute( name, 2 )
236 : elem.getAttribute( name );
238 // Non-existent attributes return null, we normalize to undefined
239 return attr === null ? undefined : attr;
242 // elem is actually elem.style ... set the style
243 // Using attr for specific style information is now deprecated. Use style insead.
244 return jQuery.style(elem, name, value);