Bug Tracker

Opened 7 years ago

Closed 6 years ago

Last modified 5 years ago

#6135 closed bug (fixed)

jQuery tries to access the .style attribute of text tags

Reported by: ryangreenberg Owned by:
Priority: low Milestone: 1.6
Component: css Version: 1.4.1
Keywords: Cc:
Blocked by: Blocking:


When constructing elements from HTML using $([html]), jQuery will sometimes include whitespace nodes. .hide and .show try to manipulate the non-existant .style property of these whitespace nodes resulting in an error.

This error occurs on Firefox 3.5 and Safari 4 for Mac. See a demonstration of this bug at http://jsbin.com/avisi/6. Here is a test case:

If you don't see "Hi" and "Hello", an error occurred.
<div id="project">
var h = $('#project').html();
alert("No error with whitespace nodes.");

Note: I know this is not a preferred way to duplicate elements on the page--this code is simply to demonstrate the problem. I discovered this problem when loading HTML fragment from the server and trying to insert them using $(html).hide().appendTo().

Attachments (1)

6135-unit-test.patch (919 bytes) - added by brandon 7 years ago.
unit test

Download all attachments as: .zip

Change History (9)

comment:1 Changed 7 years ago by ryangreenberg

I forgot to change the name of this ticket, which should be "jQuery tries to access the .style attribute of whitespace nodes"

comment:2 Changed 7 years ago by Markus.Staab

Your html ist not Valid.

comment:3 in reply to:  2 Changed 7 years ago by ryangreenberg

Replying to Markus.Staab:

Your html ist not Valid.

I'm supplying just an HTML fragment for this example. You can see in the test case that a complete doctype declaration and head is provided. If there is something invalid about the following fragment, please be more specific about the problem:

<div id="project">

Changed 7 years ago by brandon

Attachment: 6135-unit-test.patch added

unit test

comment:4 Changed 7 years ago by dmethvin

Priority: undecided
Status: newopen

comment:5 Changed 7 years ago by cowboy

Why not create an :element selector?

$.expr[":"].element = function( elem ) { return elem.nodeType === 1; };


Or an .elements() method:

$.fn.elements = function(){
  return this.pushStack( this.filter(function(){
    return this.nodeType === 1;
  }), "elements", "" );


comment:6 Changed 7 years ago by cowboy

Or something like this more generic .nodeType() method (plugin):


comment:7 Changed 6 years ago by John Resig

Resolution: fixed
Status: openclosed

Make sure that hide or show don't fail when operating on non-Element nodes. Fixes #6135.

Changeset: 21c0be8496127df3f0ad652e582133148430cc41

comment:8 Changed 6 years ago by john

Priority: undecidedlow
Note: See TracTickets for help on using tickets.