Bug Tracker

Modify

Ticket #12881 (closed bug: notabug)

Opened 18 months ago

Last modified 18 months 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 18 months ago by dandv

comment:2 Changed 18 months 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.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.