Bug Tracker

Ticket #12881 (closed bug: notabug)

Opened 2 years ago

Last modified 2 years ago

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

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

comment:1 Changed 2 years ago by dandv

comment:2 Changed 2 years ago by dmethvin

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

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.