Bug Tracker

Ticket #6562 (closed bug: fixed)

Opened 4 years ago

Last modified 3 years ago

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:
Blocking: Blocked by:

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

test.html Download (548 bytes) - added by cloakedninjas 4 years ago.

Change History

Changed 4 years ago by cloakedninjas

comment:1 Changed 4 years ago by tomgrohl

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.

comment:2 Changed 4 years ago by tomgrohl

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.

Version 0, edited 4 years ago by tomgrohl (next)

comment:3 Changed 4 years ago by snover

  • Keywords form dom added
  • Priority set to 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.

comment:4 Changed 4 years ago by snover

  • Keywords needsreview added

comment:5 Changed 4 years ago by snover

  • Milestone 1.4.3 deleted

Resetting milestone to future.

comment:6 Changed 4 years ago by tomgrohl

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.

comment:7 Changed 4 years ago by addyosmani

#8628 is a duplicate of this ticket.

comment:8 Changed 4 years ago by timmywil

  • Status changed from new to assigned
  • Priority changed from undecided to blocker
  • Owner set to timmywil
  • Version changed from 1.4.2 to git
  • Milestone set to 1.6
  • Keywords needsreview removed

comment:9 Changed 4 years ago by timmywil

  • Status changed from assigned to closed
  • Resolution set to fixed
Note: See TracTickets for help on using tickets.