Bug Tracker

Opened 11 years ago

Closed 11 years ago

#12881 closed bug (notabug)

Second argument to constructor is ignored if the element has text

Reported by: dandv Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.8.2
Keywords: Cc:
Blocked by: Blocking:

Description

http://api.jquery.com/jQuery/ states that "As of jQuery 1.4, the second argument to jQuery() can accept a map consisting of a superset of the properties that can be passed to the .attr() method."

The problem is that if the first argument (the HTML) already has a text, the second argument (the attribute map) will be ignored. I.e. $("<a>text</a>", {href: "http://jquery.com"}) won't add the href attribute.

// works as expected
$("<a>link 1</a>").attr({href: "http://link1.com", class: "red"}).appendTo("body");
    
// doesn't create any attributes
$("<a>link 2</a>", {href: "http://api.jquery.com/jQuery/", class: "red"}).appendTo("body");

// works as expected
$("<a>", {text: "link 3", href: "http://api.jquery.com/jQuery/", class: "red"}).appendTo("body");

Change History (2)

comment:1 Changed 11 years ago by dandv

comment:2 Changed 11 years ago by dmethvin

Resolution: notabug
Status: newclosed

The only HTML you can use with that signature is a simple element with no attributes, such as $("<a/>", { ... }). I strongly recommend you not use the signature at all and used chaining instead.

Note: See TracTickets for help on using tickets.