tags:

views:

68

answers:

3

I've never seen this in any jQuery docs I've read; nor, have I ever seen it in the wild. I just observed multi-content syntax working here for the after modifier with jQuery 1.4.2. Is this supported syntax? Is it deprecated?

$(".section.warranty .warranty_checks :last").after(
  $('<div class="little check" />').click( function () {
      alert('hi')                                       
  } )                                                   
  , $('<span>OEM</span>')  /*Notice this (a second) argument */    
);                                       

Here is the signature for after: .after( content ). But, as my example shows it should be .after( content [, content...] )

I've never seen any indication in the jQuery grammar that any of the functions accept more than one argument (content) in such a fashion.

UPDATE: What did it do? I left this out thinking it was obvious:

It inserts a <div class="little check" /> with the aforementioned callback on .click() and follows it up with a totally new sibling element <span>OEM</span>.

See this follow-up for a question about how to rewrite this.

A: 
SLaks
This works, right now.
Evan Carroll
It's not the `click` that is passed two arguments, it's the `after`.
Marian
+2  A: 

Yes this works, though it's not technically supported, you can see a demo here.

If you give .after() multiple arguments, it'll append them one at a time.

You can see the relevant jQuery core code here: http://github.com/jquery/jquery/blob/master/src/manipulation.js#L141

It takes all the arguments provided and pushes them on the stack for insertion...but since this isn't a documented feature, it may change in any future jQuery release (though it appears safe for at least 1.4.3).

Nick Craver
Note that if the jQuery object has no elements, it will only use the first parameter.
SLaks
Thanks for the corroboration and jsfiddle is new to me and pretty cool. Thanks for the link.
Evan Carroll
@SLaks - good point, though it would have no effect with no elements anyway :)
Nick Craver
@Nick: Look at the source. It does do something. `else if ( arguments.length )`
SLaks
@Slaks - Ah yeah, I thought you meant the set you're running the `.after()` on being empty
Nick Craver
+1  A: 

According to the docs it is not allowed, but you see in the code that it is possible (what you however already know from your experiments ;-) )

after: function() {
    if ( this[0] && this[0].parentNode ) {
        return this.domManip(arguments, false, function( elem ) {
            this.parentNode.insertBefore( elem, this.nextSibling );
        });
    } else if ( arguments.length ) {
        var set = this.pushStack( this, "after", arguments );
        set.push.apply( set, jQuery(arguments[0]).toArray() );
        return set;
    }
},
Marian
I would say it *works*, supported is a very different term :) anything not in documentation is fair game for changing
Nick Craver
You're right on that, changed.
Marian