Bug Tracker

Modify

Ticket #1169 (closed bug: duplicate)

Opened 6 years ago

Last modified 3 years ago

Selecting a form by ID fails if there's a form value element named "id" [patch]

Reported by: blueyed Owned by:
Priority: major Milestone: 1.1.3
Component: core Version: 1.1.2
Keywords: Cc:
Blocking: Blocked by:

Description

Given a FORM with ID "foo", $("#foo") will fail in Internet explorer (at least IE6) if there's a INPUT named "id". This is because "oid.id" will then refer to the (hidden) form field, but not the form's tag attribute "id" itself. It even fails when using getAttribute() explicitly.

I've fixed it like below, checking that oid.id is not an object.

Index: jquery/src/selector/selector.js
===================================================================
--- jquery/src/selector/selector.js	(Revision 1883)
+++ jquery/src/selector/selector.js	(Arbeitskopie)
@@ -229,7 +229,8 @@
 						
    // Do a quick check for the existence of the actual ID attribute
 	// to avoid selecting by the name attribute in IE
-	if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && oid.id != m[2] )
+	// NOTE: if you have a form with a hidden field "id", IE6 will select this, even when using getAttribute instead of "oid.id"
+	if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && oid.id != m[2] && typeof oid.id != "object" )
 		oid = jQuery('[@id="'+m[2]+'"]', elem)[0];
 
 	// Do a quick check for node name (where applicable) so

The patch is against SVN r1883.

Change History

comment:1 Changed 6 years ago by brandon

  • Status changed from new to closed
  • Resolution set to fixed

Fixed in SVN Rev [1896].

comment:2 Changed 3 years ago by ttrnka

  • Status changed from closed to reopened
  • Resolution fixed deleted

I'm still having problems updating/using a FORM element attribute 'id' in Firefox and Internet Explorer (jQuery 1.4.1 + tested on 1.3 and 1.3.2). See the code bellow:

Sample code

<form id="foo">
 <input name="id">
</form>
<script type="text/javascript">
alert($("#foo").attr('id'));
$("#foo").attr('id', 'bar');
alert($("#foo").attr('id'));
alert($("#bar").attr('id'));
</script>


Output

Firefox 3.5.7
foo-foo-null [wrong]

Internet Explorer 8.0
foo- ... crashes on line: $("#foo").attr('id', 'bar');

Google Chrome 4.0
foo-null-bar [correct]

comment:3 Changed 3 years ago by dmethvin

  • Status changed from reopened to closed
  • Resolution set to duplicate

I'm pretty sure this is a dup of #3113, and applies to any conflict between form attribute/properties and the names of children in the form.elements collection.

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.