Bug Tracker

Opened 13 years ago

Closed 10 years ago

#1169 closed bug (duplicate)

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

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 (3)

comment:1 Changed 13 years ago by brandon

Resolution: fixed
Status: newclosed

Fixed in SVN Rev [1896].

comment:2 Changed 10 years ago by ttrnka

Resolution: fixed
Status: closedreopened

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 10 years ago by dmethvin

Resolution: duplicate
Status: reopenedclosed

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.

Note: See TracTickets for help on using tickets.