0

We just upgraded our jQuery version from v1.7 to v3.4.1. We have code which passes a jQuery object (which doesn't yet exist in the DOM, and I'm guessing is the root cause of this problem) to a function. However, when attempting to handle this newly created variable, it only returns the root jQuery function itself: jQuery.fn.init {}

This SO answer helped me realize the issue: Help understanding jQuery's jQuery.fn.init Why is init in fn

Actual Snippet:

create: function (params) {
        console.log('params.target:', params.target);
        var $target, id;

        if (_.isString(params.target)) {
            if (params.target.charAt(0) === '#') {
                $target = $('#send-to-friend-dialog');
                console.log('$target1: ', $target);
            } else {
                $target = $('#' + params.target);
            }
        } else if (params.target instanceof jQuery) {
            $target = params.target;
        } else {
            $target = $('#dialog-container');
        }

        console.log('$target.length: ', $target.length)

        // if no element found, create one
        if ($target.length === 0) {
            console.log('$target2 : ', $target);
            if ($target.selector && $target.selector.charAt(0) === '#') {
                id = $target.selector.substr(1);
                $target = $('<div>').attr('id', id).addClass('dialog-content').appendTo('body');
            }
        }

        // create the dialog
        console.log('$target3: ', $target);
        this.$container = $target;
        this.$container.dialog($.extend(true, {}, this.settings, params.options || {}));
        return this.$container;
    },

Output in v1.12.4: https://i.stack.imgur.com/DkPtx.png

Output in v3.4.1: https://i.stack.imgur.com/2PgWR.png

So while it is defined, the object itself is a very different output in both versions and I'm wondering why?

Thank you!

1 Answers1

1

The jQuery .selector property was deprecated in 1.7, and removed in 3.0. See https://api.jquery.com/selector/. This was an internal interface, not intended to be used by applications.

You'll need to redesign your code so it doesn't need this. The documentation suggests an alternative:

For example, a "foo" plugin could be written as $.fn.foo = function( selector, options ) { /* plugin code goes here */ };, and the person using the plugin would write $( "div.bar" ).foo( "div.bar", {dog: "bark"} ); with the "div.bar" selector repeated as the first argument of .foo().

Barmar
  • 741,623
  • 53
  • 500
  • 612