Comments on: jQuery: $(elem).with(); https://j11y.io/snippets/jquery-with-method/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: James https://j11y.io/snippets/jquery-with-method/#comment-339 Wed, 07 Jan 2009 23:57:02 +0000 https://j11y.io/?p=297#comment-339 Hi John!

Thanks for clearing that up! I do understand why you removed it, although it does seem odd that it wasn’t used a lot, – probably not because it wasn’t useful but because people simply didn’t know it was there. As you know, the same functionality can be achieved in numerous other ways so I guess people didn’t even bother enquiring.

BTW, awesome work on Sizzle, it rules! 🙂

]]>
By: Marin https://j11y.io/snippets/jquery-with-method/#comment-338 Wed, 07 Jan 2009 23:51:40 +0000 https://j11y.io/?p=297#comment-338 comments are ok now. Great blog btw 🙂

]]>
By: John Resig https://j11y.io/snippets/jquery-with-method/#comment-337 Wed, 07 Jan 2009 23:10:41 +0000 https://j11y.io/?p=297#comment-337 @Dave, James: To clarify: .end() has been in jQuery since the very beginning, as has some form of a stack (although it was definitely clarified in jQuery 1.0, back in 2006).

I, personally, added in the .find(“foo”, function(){}) syntax because I liked – for the same reasons that you did – but after a while it became apparent that 1) I was the only one using it and 2) That it made a large number of methods very sloppy and ambiguous.

We can certainly look at adding it back in some day – but I’m just not seeing it, right now.

]]>
By: James https://j11y.io/snippets/jquery-with-method/#comment-336 Thu, 01 Jan 2009 11:54:39 +0000 https://j11y.io/?p=297#comment-336 Thanks Dave, that code will definitely come in handy! It still doesn’t make sense why they got rid of it though, it wouldn’t have done any harm to leave it there. Continually using ‘end()’ just seems so pointless…

]]>
By: Dave https://j11y.io/snippets/jquery-with-method/#comment-335 Wed, 31 Dec 2008 17:29:14 +0000 https://j11y.io/?p=297#comment-335 The old jQuery methods returned the original elements if you provided a function, since the function had already processed the found/filtered ones. It looks like they were removed in 1.1, January 2007. I think that was the version where the stack and .end() was introduced so maybe that’s why it was considered expendable.

If you wanted to get that functionality for all methods you could do something like this:

$().using("children", "li", function() { });

The first arg is the name of the jQuery method you want to use, followed by its arguments (if any) and finally the function to be applied to the filtered elements. A simple implementation:

$.fn.using = function(/*method, args, fn*/){
	var ap = Array.prototype, args = arguments,
		method = ap.shift.call(args), fn = ap.pop.call(args);
	return this[method].apply(this, args).each(fn).end();
};

Hilarity ensues if the method isn’t one that requires an .end() or if there aren’t enough arguments.

]]>
By: James https://j11y.io/snippets/jquery-with-method/#comment-334 Wed, 31 Dec 2008 09:48:59 +0000 https://j11y.io/?p=297#comment-334 Dave, that was my original concern; about “with” being a reserved word, but I didn’t think it mattered when used as a property/method name – it seems to work, although admittedly I haven’t yet tested it in older browsers. If not, then it can just be renamed, I only called it “with” because that seemed to make the most sense. I wasn’t aware that jQuery originally had this functionality under the ‘find’ method; I can’t imagine why they’ve removed it…

It’s easy enough to bring back though:

$.fn.oFind = $.fn.find;
$.fn.find = function(){
    var a = arguments;
    return $.isFunction(a[1]) ?
        // If a function is passed don't return
        // elems, just perform the action on them:
        this.each(function(){
            $(a[0],this).each(a[1]);
        })
        : this.oFind.apply(this,a);
}

The problem now is that it doesn’t quite make sense… should it return the original element or the new-found elements? The point in the “with” method was that it didn’t change the subject of the chain…

]]>
By: Dave https://j11y.io/snippets/jquery-with-method/#comment-333 Wed, 31 Dec 2008 02:10:19 +0000 https://j11y.io/?p=297#comment-333 I like the idea, but “with” is a Javascript reserved word…

Early versions of jQuery allowed find and filter to take a second optional argument that was a function. When that was provided it would do an each with that function using the find/filter elements and not change the chain. So your example above would just use find instead of with and it would work just the way you want.

I don’t recall why that feature was removed; I know it was the subject of some extended discussion on a jQuery list at the time.

]]>
By: James https://j11y.io/snippets/jquery-with-method/#comment-332 Tue, 30 Dec 2008 23:03:40 +0000 https://j11y.io/?p=297#comment-332 Great! I’m glad you all like it! 🙂

]]>
By: ignite https://j11y.io/snippets/jquery-with-method/#comment-331 Tue, 30 Dec 2008 19:44:14 +0000 https://j11y.io/?p=297#comment-331 Very nice! I really enjoy these tips. I’m still learning jQuery so your explanations and examples have been really helpful. Thanks!

]]>
By: Ibrahim https://j11y.io/snippets/jquery-with-method/#comment-330 Tue, 30 Dec 2008 19:44:10 +0000 https://j11y.io/?p=297#comment-330 Very impressive, thanks James 😉

]]>