1

Is there any way to get the selector used to call my widget?

For example, if my selector is $("input [Title*='Cost Center']"), I want my widget to be able to see "input [Title*='Cost Center']".

I found this post Accessing .selector within a JQuery UI Widget which might suggest how it can be done but cannot begin to understand it. If this is an answer how do I implement it? My widget name is PNC_ERA.simpleDialogListSP.

1 Answer 1

1

check this out:

HTML

<div id='test'></div>
<div class='test'></div>

Widget

$(function () {
    $.widget("test.testwidget", {
        _create: function () {
            this.element.text('Selector: ' + this.options.selector)
        },
    });
}(jQuery));

and the code from the answer that you found already

// Save the original factory function in a variable
var testFactory = $.fn.testwidget;

// Replace the function with our own implementation
$.fn.testwidget = function(obj){
    // Add the selector property to the options
    $.extend(obj, { selector: this.selector });

    // Run the original factory function with proper context and arguments
    return testFactory.apply(this,arguments);
};

Usage

$(document).ready(function() {
    $('#test').testwidget({});
    $('.test').testwidget({});
});

Here is working JSFiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This is exactly what I needed to finish my project. I guess I will need to deep dive into widget factory to see what it is doing. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.