Merging jQuery objects

.add() does exactly what you’re after.

h3.add(btn).hide();

If you wanted to make it a little more convenient for yourself, with a “merge” function like in your question, this could be added easily:

$.merge = function(objs) {
    var ret = objs.shift();
    while (objs.length) {
        ret.add(objs.shift());
    }
    return ret;
};

$.merge([h3, btn]).hide()

Leave a Comment