Ticket #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: | ||
| Blocking: | Blocked by: |
Description
If form has input with name="id" its id automatically become the input element.
Change History
comment:1 Changed 3 years ago by SlexAxton
- Status changed from new to closed
- Resolution set to patchwelcome
comment:2 Changed 3 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
comment:3 Changed 3 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.
http://www.w3schools.com/Dom/dom_nodetype.asp
NodeType Named Constant
1 ELEMENT_NODE
2 ATTRIBUTE_NODE
comment:4 Changed 3 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
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.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

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.