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 // Typecast once if the value is a number
86 if ( typeof value === "number" )
91 return this.each(function(){
92 if(jQuery.isFunction(value)) {
93 val = value.call(this);
94 // Typecast each time if the value is a Function and the appended
95 // value is therefore different each time.
96 if( typeof val === "number" ) val += '';
99 if ( this.nodeType != 1 )
102 if ( jQuery.isArray(val) && /radio|checkbox/.test( this.type ) )
103 this.checked = (jQuery.inArray(this.value, val) >= 0 ||
104 jQuery.inArray(this.name, val) >= 0);
106 else if ( jQuery.nodeName( this, "select" ) ) {
107 var values = jQuery.makeArray(val);
109 jQuery( "option", this ).each(function(){
110 this.selected = (jQuery.inArray( this.value, values ) >= 0 ||
111 jQuery.inArray( this.text, values ) >= 0);
114 if ( !values.length )
115 this.selectedIndex = -1;
124 removeAttr: function( name ) {
125 jQuery.attr( this, name, "" );
126 if (this.nodeType == 1)
127 this.removeAttribute( name );
130 addClass: function( classNames ) {
131 jQuery.className.add( this, classNames );
134 removeClass: function( classNames ) {
135 jQuery.className.remove( this, classNames );
138 toggleClass: function( classNames, state ) {
139 var type = typeof classNames;
140 if ( type === "string" ) {
141 // toggle individual class names
142 var isBool = typeof state === "boolean", className, i = 0,
143 classNames = classNames.split( /\s+/ );
144 while ( (className = classNames[ i++ ]) ) {
145 // check each className given, space seperated list
146 state = isBool ? state : !jQuery.className.has( this, className );
147 jQuery.className[ state ? "add" : "remove" ]( this, className );
149 } else if ( type === "undefined" || type === "boolean" ) {
150 if ( this.className ) {
151 // store className if set
152 jQuery.data( this, "__className__", this.className );
154 // toggle whole className
155 this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
158 }, function(name, fn){
159 jQuery.fn[ name ] = function(){
160 return this.each( fn, arguments );
166 // internal only, use addClass("class")
167 add: function( elem, classNames ) {
168 jQuery.each((classNames || "").split(/\s+/), function(i, className){
169 if ( elem.nodeType == 1 && !jQuery.className.has( elem.className, className ) )
170 elem.className += (elem.className ? " " : "") + className;
174 // internal only, use removeClass("class")
175 remove: function( elem, classNames ) {
176 if (elem.nodeType == 1)
177 elem.className = classNames !== undefined ?
178 jQuery.grep(elem.className.split(/\s+/), function(className){
179 return !jQuery.className.has( classNames, className );
184 // internal only, use hasClass("class")
185 has: function( elem, className ) {
186 return elem && jQuery.inArray( className, (elem.className || elem).toString().split(/\s+/) ) > -1;
190 attr: function( elem, name, value ) {
191 // don't set attributes on text and comment nodes
192 if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
195 var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
196 // Whether we are setting (or getting)
197 set = value !== undefined;
199 // Try to normalize/fix the name
200 name = notxml && jQuery.props[ name ] || name;
202 // Only do all the following if this is a node (faster for style)
203 if ( elem.nodeType === 1 ) {
205 // These attributes require special treatment
206 var special = /href|src|style/.test( name );
208 // Safari mis-reports the default selected property of a hidden option
209 // Accessing the parent's selectedIndex property fixes it
210 if ( name == "selected" && elem.parentNode )
211 elem.parentNode.selectedIndex;
213 // If applicable, access the attribute via the DOM 0 way
214 if ( name in elem && notxml && !special ) {
216 // We can't allow the type property to be changed (since it causes problems in IE)
217 if ( name == "type" && /(button|input)/i.test(elem.nodeName) && elem.parentNode )
218 throw "type property can't be changed";
220 elem[ name ] = value;
223 // browsers index elements by id/name on forms, give priority to attributes.
224 if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
225 return elem.getAttributeNode( name ).nodeValue;
227 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
228 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
229 if ( name == "tabIndex" ) {
230 var attributeNode = elem.getAttributeNode( "tabIndex" );
231 return attributeNode && attributeNode.specified
232 ? attributeNode.value
233 : /(button|input|object|select|textarea)/i.test(elem.nodeName)
235 : /^(a|area)$/i.test(elem.nodeName) && elem.href
243 if ( !jQuery.support.style && notxml && name == "style" ) {
245 elem.style.cssText = "" + value;
247 return elem.style.cssText;
251 // convert the value to a string (all browsers do this but IE) see #1070
252 elem.setAttribute( name, "" + value );
254 var attr = !jQuery.support.hrefNormalized && notxml && special
255 // Some attributes require a special call on IE
256 ? elem.getAttribute( name, 2 )
257 : elem.getAttribute( name );
259 // Non-existent attributes return null, we normalize to undefined
260 return attr === null ? undefined : attr;
263 // elem is actually elem.style ... set the style
264 // Using attr for specific style information is now deprecated. Use style insead.
265 return jQuery.style(elem, name, value);