Skip to main content

Bug Tracker

Side navigation

#6562 closed bug (fixed)

Opened May 13, 2010 08:55AM UTC

Closed April 10, 2011 08:01PM UTC

Last modified March 09, 2012 04:01AM UTC

using .attr() to set the 'target' attribute, with a node that has ID of 'target'

Reported by: cloakedninjas Owned by: timmywil
Priority: blocker Milestone: 1.6
Component: attributes Version: git
Keywords: form dom Cc:
Blocked by: Blocking:
Description

Code to replicate is attached.

Essentially, if you have a Dom node with the ID of 'target' and you try and set a target, it fails.

If you try to just get the target attribute when there isn't one, the 'target' node is returned. If the target attribute is present, then the getter works.

If you comment out the <input> element, the code runs fine

Attachments (1)
  • test.html (0.5 KB) - added by cloakedninjas May 13, 2010 08:55AM UTC.
Change History (9)

Changed October 12, 2010 08:55AM UTC by tomgrohl comment:1

From what I've seen, internally jQuery is doing main_form.target (elem[ name ]) to work in all browsers, which is why the node 'target' is accessed and not the attribute.

I've found that if the type of main_form.target is not a string ( so in this case it would be a object ) you could use a different method.

I found the best way ( albeit with a for loop ) that works in all major browsers was to change line 1704 ( needs simplifying a little bit ) to:

if ( jQuery.type(elem[ name ]) === "string" )
{
	elem[ name ] = value;
}
else
{
	for ( var  i=0; i < elem.attributes.length; i++ ){
		if ( elem.attributes[i].name == name )
		{
			elem.attributes[i].value = value;
		}
	}	
}

So if the attribute it tries to access is a Dom object and not a string it uses a standard way that will work.

I've used a for loop to access the attributes as setAttribute does not work in all browsers.

Changed October 13, 2010 11:48AM UTC by tomgrohl comment:2

_comment0: Actually using: \ \ {{{ \ elem.getAttributeNode( name ).nodeValue = value; \ }}} \ \ might be better than: \ \ {{{ \ elem[ name ] = value; \ }}} \ \ to avoid cases where an element id/name can be a attribute of the same or another element. Seems to work in all browsers too. \ 1288078559447626

Actually using:

elem.getAttributeNode( name ).nodeValue = value;

might be better than:

elem[ name ] = value;

to avoid cases where an element id/name can be a attribute of the same or another element. Seems to work in all browsers too.

Edit:

jQuery already uses elem.getAttributeNode( name ).nodeValue for getting an attribute value when an element is a form, don't see why it can't use it for setting too.

Changed October 15, 2010 09:30PM UTC by snover comment:3

keywords: → form dom
priority: → undecided

This needs to be correlated with other bugs related to the DOM providing access to form elements as direct properties of the form object.

Changed October 15, 2010 09:30PM UTC by snover comment:4

keywords: form domform dom needsreview

Changed November 12, 2010 02:39AM UTC by snover comment:5

milestone: 1.4.3

Resetting milestone to future.

Changed March 24, 2011 09:11PM UTC by tomgrohl comment:6

One way to is to check whether the element being accessed is a form.

In 1.5.1 something like this is done for getting an attribute, so it might be good to use for setting.

I changed from line 2062 (ish) from:


if ( value === null ) {
	if ( elem.nodeType === 1 ) {
		elem.removeAttribute( name );
	}

} else {
	elem[ name ] = value;
}

to:


if ( value === null ) {
	if ( elem.nodeType === 1 ) {
		elem.removeAttribute( name );
	}

} else {
	
	if ( set && jQuery.nodeName( elem, "form" ) ) {
		elem.setAttribute( name, value );
	} else {
		elem[ name ] = value;
	}
}

And it works fine. Only tested in Firefox so needs to be tested in other browsers but I believe it should work.

Changed March 24, 2011 10:32PM UTC by addyosmani comment:7

#8628 is a duplicate of this ticket.

Changed March 31, 2011 06:59AM UTC by timmywil comment:8

keywords: form dom needsreviewform dom
milestone: → 1.6
owner: → timmywil
priority: undecidedblocker
status: newassigned
version: 1.4.2git

Changed April 10, 2011 08:01PM UTC by timmywil comment:9

resolution: → fixed
status: assignedclosed