Bug Tracker

Modify

Ticket #8348 (closed bug: invalid)

Opened 3 years ago

Last modified 3 years ago

jQuery.fn.text on a textNode

Reported by: jv215@… Owned by:
Priority: low Milestone: 1.next
Component: manipulation Version: 1.5
Keywords: Cc:
Blocking: Blocked by:

Description

When calling

$(document.createTextNode("foo")).text("bar").appendTo(document.body)

The text is set to "foo" instead of "bar". One would expect that you can set the text on a textNode using .text()

see jsFiddle:  http://jsfiddle.net/Raynos/PU4PX/2/

The problem lies here.

if ( typeof text !== "object" && text !== undefined ) {
    return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
}

An assumption was made that you can append a text node to a text node. Which is invalid because textNodes can't have children.

One possible solution is checking for this.nodeType === 3 and using elem.nodeValue = text.

An alternative would be using textNode.data as seen here:  http://jsfiddle.net/PU4PX/3/

I've only tested nodeValue in internet explorer and firefox.

Change History

comment:1 Changed 3 years ago by rwaldron

  • Priority changed from undecided to low
  • Status changed from new to open
  • Component changed from unfiled to manipulation

comment:2 Changed 3 years ago by dmethvin

  • Status changed from open to closed
  • Resolution set to invalid

Text and comment nodes have only limited support; in most cases we attempt to remove them before performing any operations (for example after a .contents() method). Don't attempt to wrap a text node in a jQuery object.

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.