tags:

views:

86

answers:

4

When you create an element by passing HTML to jQuery, there's a number of formats you could use, for example:

$("<span>").addClass("foo")
$("<span class=\"foo\"></span>")

etc...

Is there one style which works better than others?

+1  A: 

I previously thought there was no difference, but I was looking at the code which hks posted in this other question, and it showed some inconsistencies in IE. To investigate, I've put together a table.

Take a look at the results here: http://fisher.spadgos.com/stuff/jqueryConstructors.html

Basically it's things like this:

$('<a class="foo">').length; // in firefox, this == 1. IE, this == 0

$('<br></br>').length; // Firefox == 1; IE == 2

In IE it's very inconsistent, however in Firefox and Chrome all forms work pretty well.

The only methods which were consistent across all browsers were these:

$("<foo />")    $("<foo>")

From the manual:

Create DOM elements on-the-fly from the provided String of raw HTML.

Simple elements without attributes, e.g., "<div />", are created via document.createElement. All other cases are parsed by assigning the string to the .innerHTML property of a div element. The HTML string cannot contain elements that are invalid within a div, such as html, head, body, or title elements.

All HTML must be well-formed, otherwise it may not work correctly across all browsers. This includes the case where $("<span>") may not work but $("<span/>") will (note the XML-style closing slash).

nickf
You're getting unpredictable results because you're using malformed HTML. <br></br> is not correct HTML 4. And your anchor is not closed. That being said, it might still be a bug but the malformed nature largely invalidates the results.
cletus
yes, but if you do this: `$("<a>")`, then that will work. I was just showing that the results are a bit unpredictable in IE.
nickf
Seems like what you are seeing is how the browsers "clean up" malformed HTML. They do this differently, which is one of the big reasons to use "proper" markup. They also may do this differently if they are dealing with different doctypes; "quirks mode" is one of these, but it's really more complicated.Not following the guidelines of the jQuery docs can create some fairly nasty bugs, as your HTML can get re-written on you. This causes Ajax pages to break in weird ways in different browsers.
ndp
A: 

I can't say for certain, but performance wise the second option:

$("<span class=\"foo\"></span>");

is generally better because there's no extra string concatenation (i.e. adding the "class='foo'" into matching span tags.

You also have the benefit of viewing it in the source using the default FF/IE/OPERA/ETC source viewers, whereas you'll need to look at a live view of the DOM to see the result of this:

$("<span>").addClass("foo")

Pick the second option - static HTML carries a minor performance benefit but more importantly a serious readability benefit that will help you when you ultimately hit 'View Source' to try and figure out why your elements aren't showing up as desired.

jscharf
A: 

First create a div element like the following:

<div id="test"></div>

Then you can easily do this using the following code:

$("<span class=\"foo\"></span>").appendTo("div#test");

You can also use any other html element such as table instead of div.

Hope this would help you.

Arka Chatterjee
that doesn't work as expected in IE for elements which aren't expected to have closing tags: `area, br, col, hr, img, input, link, meta, param, title`. yes, you could just leave out the closing tag, but you're safer going with the short version.
nickf
A: 

Given that your actual question is about style and which is prefered I have put in an answer for 'neither' and throw down an awesome plugin I found recently that does a brilliant job of maintaining seperation of HTML and javascript. The plugin is called tempest

Steerpike