0

I'm writing my first jquery UI widget: a date range control composed of two text boxes (start and end date) both of which will be using the jQuery UI DatePicker.

All the widget code examples I've seen online have you applying the widget behavior to a generic selector. In my case, I need exactly two elements in my selector (the two aforementioned text boxes) and will be applying different logic to each. For example, the start date text box will have different onblur events/behavior than the end date text box.

Is there a way to create a widget without a selector and instead have two named constructor arguments, one for start date and one for end date? If not, is there a better way I should be approaching this widget? Perhaps I should create a POJSO (plain old JS object) instead?

1 Answer 1

1

what's it matter what the selector is? widgets use selectors because most of the time it applies to the selected elements. but it's up to the widget to use the selected elements. technically, it could just ignore them and continue processing whatever. so why don't you just select the document (or like the parent container of the textboxes), and pass in an object as the parameter to your widget with values for "textbox1" and "textbox2" and then get those values on initialization. does that make sense?

for example:

$("#parent_container").yourWidget({
    txtbox1: $("#txt_id1"),
    txtbox2: $("#txt_id2"),
    otherParam: true
});

and in your widget code, just ignore selected elements. process everything based on the parameter's values.

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

2 Comments

It doesn't matter to me at all! I'm just worried that the widget base class uses the selector to some kind of internal state or to enforce the association between the JS objects and the DOM. I assume this is not a concern?
No, not a concern. You can pass the second selector as a parameter. For instance for your date range you would have a FROM and a TO input field. $("#from").dateRange("#to", otherOptions);

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.