Skip to main content

Bug Tracker

Side navigation

#12881 closed bug (notabug)

Opened November 11, 2012 08:07AM UTC

Closed November 11, 2012 02:54PM UTC

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");
Attachments (0)
Change History (2)

Changed November 11, 2012 08:15AM UTC by dandv comment:1

Here's the jsFiddle: http://jsfiddle.net/dandv/4vhmZ/1/

Changed November 11, 2012 02:54PM UTC by dmethvin comment:2

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.