Bug Tracker

Opened 10 years ago

Closed 10 years ago

#12708 closed bug (fixed)

properties hash ignored when attributes included in html

Reported by: spudly Owned by:
Priority: low Milestone: None
Component: core Version: 1.8.2
Keywords: Cc:
Blocked by: Blocking:


When creating DOM elements with $(), if you include any attributes in the passed html, a passed properties hash will be ignored:

Code: $('<input type="hidden" />', {name: 'foo', value: 'bar'});

Expected Result: <input type="hidden" name="foo" value="bar" />

Actual Result: <input type="hidden" />

This happens regardless of what the attribute is. It just so happens in this case that you can't take the attribute off or else you'll break IE compatibility.

Change History (4)

comment:1 Changed 10 years ago by dmethvin

Yes, that's a known limitation to the $(html, props) signature. I think we should keep it that way because it is not a good signature IMO. Perhaps we can warn people away from it more strongly in the docs, I don't think we have done that adequately. Your example looks about the same when done in a non-ambiguous .attr():

$('<input type="hidden" />').attr({name: 'foo', value: 'bar'});

comment:2 Changed 10 years ago by timmywil

Component: unfiledcore
Priority: undecidedlow
Status: newopen

Confirmed. We actually document that including type is an exception to the single-element rule.

However, our docs are wrong. IE won't break here. input type can be set on creation before connected to the DOM.


We need to update the docs here: http://api.jquery.com/jQuery/#jQuery2. I think that was fixed in 1.5.

comment:4 Changed 10 years ago by dmethvin

Resolution: fixed
Status: openclosed

The docs issue has been fixed, and it is now very clear that a simple element with no other markup must be provided. http://api.jquery.com/jQuery/#jQuery2

Note: See TracTickets for help on using tickets.