tags:

views:

27

answers:

1

In version 1.3.2 of jQuery, the following works:

$('<span/>').append(
  $([
    $('<span/>').append(
      $('<a>').attr('href', 'http://google.com').text('Google')
    ),
    $('<span/>').text('Foo')
  ])
)

It seems that in jQuery 1.4.2, the $([...]) fails silently. I need this code to be compatible with both versions of jQuery since our unit tests are forced to run 1.3.2, but the main application uses jQuery 1.4.2. Any ideas?

A: 

Try using .get(0) to grab the actual element to be added to the array. Seems to work:

Try it out: http://jsfiddle.net/TFY22/

(You can switch the jQuery version on the left in jsFiddle, then click Run at the top.)

var test = $('<span/>').append(
  $([
    $('<span/>').append(
      $('<a>').attr('href', 'http://google.com').text('foo')
    ).get(0),  // Get DOM element
    $('<span/>').text('Google').get(0)// Get DOM element
  ])
);

$('body').append(test);

EDIT: With regard to the comments below, there are simpler approaches.

This will create the entire structure at once.

$('<span><span><a href="http://google.com"&gt;foo&lt;/a&gt;&lt;/span&gt;&lt;span&gt;Google&lt;/span&gt;&lt;/span&gt;');

If you have any dynamic data to include, you can concatenate it in, just be aware that there are security concerns if the source of the data is unknown.

http://jsfiddle.net/TFY22/1/

var text = 'foo';

$('<span><span><a href="http://google.com"&gt;' + text + 
                                 '</a></span><span>Google</span></span>');

Or take a more verbose approach, and create each element separately, and append:

http://jsfiddle.net/TFY22/2/

var $top = $('<span/>');
var $a = $('<a/>').attr('href','http://google.com').text('foo');

$('<span/>').append($a).appendTo($top);
$('<span/>').text('Google').appendTo($top);

Or chain your .append() calls to give a structured appearance:

http://jsfiddle.net/TFY22/4/

var test = $('<span/>')
    .append($('<span/>')
            .append($('<a>').attr('href', 'http://google.com').text('foo'))
           )
    .append($('<span/>').text('Google'));

$('body').append(test);​
patrick dw
It certainly does work. I just wish it wasn't such an eyesore
YourParadigm
@YourParadigm - Well, it is not that much more of an eyesore than the original. Just added the two `.get(0)` calls. Is there a particular reason why you took this approach in the first place as opposed to something cleaner?
patrick dw
@patrick What would you have done to make it cleaner? "Cleanest" is what I'm trying to achieve.
YourParadigm
@Your - That would depend on how dynamic you need it to be, and what sort of security concerns there are (with regard to the source of any dynamic data). I'll update with an example or two.
patrick dw
@patrick - I'm using this as for the autocomplete formItem function. Data coming from the server should be clean, but I don't want to risk it not being so. I'd like to avoid a bunch of string concatenations, too.
YourParadigm
@patrick - The advantage to my method is that you can see the structure of the markup by the indentation being used. Neither of your added approaches accomplishes that.
YourParadigm
@Your - No, you're right. If you find your way to be more readable, then you should stick with that. You can always use `[0]` instead of `.get(0)` if you want to clean it up a little. http://jsfiddle.net/TFY22/3/
patrick dw
@Your - Of course, if you chain your `.append()` calls, you can clean it up a bit, and maintain something of a structured view. I'll add another update at the bottom. http://jsfiddle.net/TFY22/4/
patrick dw
I stumbled upon this method for doing it because I hated chaining the appends. =/
YourParadigm