Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#7472 closed bug (fixed)

$('form').attr('id') return DOM Element

Reported by: pinal Owned by:
Priority: low Milestone: 1.6
Component: attributes Version: 1.5.2
Keywords: Cc:
Blocked by: Blocking:



If form has input with name="id" its id automatically become the input element.

Change History (7)

comment:1 Changed 12 years ago by SlexAxton

Resolution: patchwelcome
Status: newclosed

If I remember correctly, some IEs get confused in forms when you have names that conflict with attribute types, since IE gives you the option of traversing a form via the element names as expandos. This is not the case in most modern browsers.

I believe the best solution is to avoid using attribute names and your name value. We could potentially find a work-around to this problem and get it in, but at the cost of performance and file-size that probably aren't worth the fix.

If you have a patch for this that you'd like us to consider, we'd be happy to work with you on it, and then ultimately see if it would make sense to go into the library, but for now, I think we're intentionally not working on this, due to obscurity and ease of a workaround.

comment:2 Changed 12 years ago by pinal

Yoy are right about IE. I didn't wrote the browser, beacause bug is in all modern browsers (chrome 7,safari 5,opera 10.6*,ff 3.6.*)
http://jsfiddle.net/ua4kZ/7/ - look
$('form').attr('id') - return node
$('form')[0].getAttribute('id') - return null

Last edited 12 years ago by pinal (previous) (diff)

comment:3 Changed 12 years ago by pinal

I think it will be right to get elem.nodeType for attribute and if nodeType !== 2, return null. So for form.id.nodeType will be equal 1 in my example.

NodeType Named Constant

Last edited 12 years ago by pinal (previous) (diff)

comment:4 Changed 12 years ago by jitter

This is a well known problem (References)
Deadly Expandos
Unsafe Names for HTML Form Controls

The problem comes from the old DOM 0/DOM 1 days. DOM 1 HTML states

The FORM element encompasses behavior similar to a collection and an element. It provides direct access to the contained input elements as well as the attributes of the form element.

And basically all browsers still do that as many scripts rely on this behavior. jQuery has already fixed many of the problems this brings when working with form (e.g. recently #7212)

But I'm not really sure we should fix it for attr(). When an explicit id is specified on the form jQuery returns that, if no id is specified attr() returns the element with id/name = id

test case

But your idea for a fix to check for nodeType or nodeName is interesting. I don't know if this fits well into jQuery/Sizzle core but it could be documented on attr() api docs.

comment:5 Changed 12 years ago by snover

#4299 is a duplicate of this ticket.

comment:6 Changed 12 years ago by timmywil

Milestone: 1.5
Resolution: patchwelcome
Status: closedreopened

comment:7 Changed 12 years ago by timmywil

Component: unfiledattributes
Milestone: 1.6
Priority: undecidedlow
Resolution: fixed
Status: reopenedclosed
Version: 1.4.4rc1.5.2
Note: See TracTickets for help on using tickets.