Wow. I leave my computer for 24 hours and I get answers from John Resig,
Yehuda Katz and Mike Alsup. I love the jQuery community (and jQuery itself,
of course).

Mike:
  Yes, adding to an empty array then jQuery'ing it would work; it's how I
originally structured my code. Adding to a jQuery better matches the program
logic to what I'm trying to accomplish; my definition of "elegant." I don't
have a problem writing $([]); i just wondered if there was a better way.

Yehuda:
  I do know about $('<..html/>'), but my mother always said "Don't hang with
those innerHTML kids, you'll learn bad habits." Seriously, I find it very
hard to debug the assemble-a-string-and-have-the-interpreter-figure-it-out
sort of thing, like innerHTML and eval. I'm not a strict standardista, but
using actual code and HTML lets Firebug or the validator find my stupid
syntax errors much more easily.

John:
  I believe you on the $() thing; I'll stick to $([]). It clearly could be
coded differently, either Mike's way or:
    var result;
    $.each(...
      if (result) result.add(e); else result = e;
    )
but again, I think my way is more elegant.

The original code is my variant of Kevin's DOM creation code
(http://mg.to/2006/02/27/easy-dom-creation-for-jquery-and-prototype#comment-294)
(yes, yet another DOM creator):

$.dom = function(){
  var result = $([]), e;
  for (var i = 0; i < arguments.length; ++i){
    if (arguments[i].constructor == String){
      // a string is either a text literal or a tag name of an element.
      // distinguish by the following argument; a tag name will be followed
by an Object
      if (arguments[i+1] && arguments[i+1].constructor == Object){
        e = $(document.createElement(arguments[i])).
          attr (arguments[++i]).css(arguments[i].style || {}); //
incorporate the attributes Object
        // append children if the next argument is an array
        if (arguments[i+1] && arguments[i+1].constructor == Array)
e.append($.dom.apply(null, arguments[++i]));
      }else{
        e = document.createTextNode(arguments[i]);
      }
    }else if (arguments[i].constructor == Array){
      // process the array
      e = $.dom.apply(null, arguments[i]);
    }else{
      // Something else . Let jQuery deal with it
      e = arguments[i];
    } // if
    result = result.add(e);
  }  // for
  return result;
}; // dom

This version allows you to insert pre-existing things in the created
elements, like
$.dom('div', {Class: 'x'}, [ 'These are all the P elements:' [$('P')]])
puts $('P') into the new div.

It also lets the attribute object include inline styles, as in
$.dom('div', {id: 'myDiv', style: {border: '1px solid red'}})
by using .css(arguments[i].style || {});

Would it be worth changing .attr to allow this directly?

You can see this in action at http://youngisrael-stl.org ; look at the
results from search box on the left column.

i guess you can add that to the list of jQuery sites, too. I'd be flattered.

Thanks for your comments.

Danny
    

John Resig wrote:
> 
> More code depends on $() referring to document than you might think.
> Whenever no context is passed in to a jQuery object, the context is
> set to 'document'. That way when you do a .find(), it'll actually find
> some relevant elements. Since $() has no context, the context then
> defaults to document. Same with doing $("").
> 
> If you want an empty set, $([]) is the preferred solution (it's what I
> would use, too).
> 
> All that being said, however, I somehow suspect that the result that
> you're trying to achieve could be done much more easily, in another
> way. Can you post your full code for us to look at? I don't think I've
> ever seen a true need to have an empty jQuery set returned.
> 
> --John
> 

-- 
View this message in context: 
http://www.nabble.com/Creating-an-empty-jQuery-object-tf3240592.html#a9026722
Sent from the JQuery mailing list archive at Nabble.com.


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to