Skip to main content

Bug Tracker

Side navigation

#3058 closed enhancement (invalid)

Opened June 18, 2008 10:29AM UTC

Closed July 23, 2008 04:28PM UTC

Last modified March 14, 2012 09:02PM UTC

$.attr() improvement ?

Reported by: arachnotech Owned by:
Priority: minor Milestone: 1.3
Component: core Version: 1.2.6
Keywords: attr removeAttr Cc:
Blocked by: Blocking:
Description

I am wondering why invoking $.attr('testAttribute', null) doesn't remove the 'testAttribute' attribute and sets it to 'null' (string) instead.

I think that it would be clever (but maybe i'm wrong ;-) to allow attribute removal using null values as it is the value natively returned when the attribute doesn't exist.

That way we could remove many attributes with a single attr({...}) call.

The $.attr() function may look like this :

	attr: function( elem, name, value ) {
		// don't set attributes on text and comment nodes
		if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
			return undefined;

		var notxml = !jQuery.isXMLDoc( elem ),
			// Whether we are setting (or getting)
			set = value !== undefined,
			msie = jQuery.browser.msie;

		// Try to normalize/fix the name
		name = notxml && jQuery.props[ name ] || name;

		// Only do all the following if this is a node (faster for style)
		// IE elem.getAttribute passes even for style
		if ( elem.tagName ) {

			// These attributes require special treatment
			var special = /href|src|style/.test( name );

			// Safari mis-reports the default selected property of a hidden option
			// Accessing the parent's selectedIndex property fixes it
			if ( name == "selected" && jQuery.browser.safari )
				elem.parentNode.selectedIndex;

			// If applicable, access the attribute via the DOM 0 way
			if ( name in elem && notxml && !special ) {
				if ( set ){
					// We can't allow the type property to be changed (since it causes problems in IE)
					if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
						throw "type property can't be changed";

					elem[ name ] = value;
				}

				// browsers index elements by id/name on forms, give priority to attributes.
				if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
					return elem.getAttributeNode( name ).nodeValue;

				return elem[ name ];
			}

			if ( msie && notxml &&  name == "style" )
				return jQuery.attr( elem.style, "cssText", value );

			if ( set )
				if(value === null) 
					// removeAttr if value === null
					elem.removeAttribute(name);
				else
					// convert the value to a string (all browsers do this but IE) see #1070
					elem.setAttribute( name, "" + value );

			var attr = msie && notxml && special
					// Some attributes require a special call on IE
					? elem.getAttribute( name, 2 )
					: elem.getAttribute( name );

			// Non-existent attributes return null, we normalize to undefined
			return attr === null ? undefined : attr;
		}

		// elem is actually elem.style ... set the style

		// IE uses filters for opacity
		if ( msie && name == "opacity" ) {
			if ( set ) {
				// IE has trouble with opacity if it does not have layout
				// Force it by setting the zoom level
				elem.zoom = 1;

				// Set the alpha filter to set the opacity
				elem.filter = (elem.filter || "").replace( /alpha\\([^)]*\\)/, "" ) +
					(parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
			}

			return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
				(parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '':
				"";
		}

		name = name.replace(/-([a-z])/ig, function(all, letter){
			return letter.toUpperCase();
		});

		if ( set )
			elem[ name ] = value;

		return elem[ name ];
	},
Attachments (0)
Change History (4)

Changed July 01, 2008 03:11AM UTC by flesler comment:1

So where's the change ? I can't spot it.

Can you make a diff ? you make a checkout from the trunk, add the modifications (save) and you do (from trunk)

svn diff > attr.diff

That will generate attr.diff, you need to upload it. Thanks

Changed July 01, 2008 03:11AM UTC by flesler comment:2

Ah, also note that we have $().removeAttribute(). So this new functionality seems useless.

Changed July 01, 2008 03:12AM UTC by flesler comment:3

Meant removeAttr... sorry.

Changed July 23, 2008 04:28PM UTC by flesler comment:4

resolution: → invalid
status: newclosed

No need for duplicated functionality, removeAttr should do just fine.