Bug Tracker

Modify

Ticket #7472 (closed bug: fixed)

Opened 3 years ago

Last modified 2 years ago

$('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

 http://jsfiddle.net/ua4kZ/1/

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

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 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

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

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[[BR]] NodeType Named Constant 1 ELEMENT_NODE 2 ATTRIBUTE_NODE

Version 0, edited 3 years ago by pinal (next)

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

 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 3 years ago by snover

#4299 is a duplicate of this ticket.

comment:6 Changed 3 years ago by timmywil

  • Status changed from closed to reopened
  • Resolution patchwelcome deleted
  • Milestone 1.5 deleted

comment:7 Changed 3 years ago by timmywil

  • Status changed from reopened to closed
  • Component changed from unfiled to attributes
  • Priority changed from undecided to low
  • Version changed from 1.4.4rc to 1.5.2
  • Milestone set to 1.6
  • Resolution set to fixed

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.