2

Is there a way to know if a textfield is an instance of jQuery UI Datepicker?

In example, lets say I have this code:

$('#item_id').datepicker();

Is there a way to do something like this:

if($('#item_id') IS NOT A DATEPICKER YET)
{
    $('#item_id').datepicker();
}

The above conditional check, of course, is in human language and not JavaScript regular code ;)

PS: I know my question is very general, but my English are not so good :(

5
  • 1
    Even without good English, you can use code examples to demonstrate what you're looking for. You'll get better community response, and better answers. :) Commented May 10, 2012 at 6:54
  • Unfortunately I dont have some code to provide. Just I like to know if there is some way to know if a textfield is already assigned to datepicker plugin Commented May 10, 2012 at 6:55
  • 1
    Not sure how you expect anyone to answer something so vague. Have you read the documentation? Maybe you could at least link that in your question? As it stands, this really doesn't help the community help you. Commented May 10, 2012 at 6:59
  • @jmort253 what I do is just the $('element_selector').datepicker(); and What I need, is to check before that if the element is already a datepicker with some conditional check. Commented May 10, 2012 at 7:01
  • 1
    Merianos - I'm a programmer, so code is the easiest way to communicate. Your example made it a lot clearer and makes sense now. +1 Great question. Commented May 10, 2012 at 7:18

1 Answer 1

4

Once a textfield has been applied with a JQuery UI Datepicker, it will contain a 'hasDatepicker' class.

Perhaps you can form a query like $('#field').hasClass('hasDatepicker') to check if the a specific textfield with id 'field' has a datepicker feature applied on it. Make sure you do the check after the datepicker has been applied, not before.

Alternatively, you can just do a check of the same classes used to apply the datepicker.

If you use $(".applyDatepicker").datepicker(); to apply the datepicker on all textfield with 'applyDatepicker' class. Then you can just backtrack with $('#field').hasClass('applyDatepicker').

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

1 Comment

Thanks !!! Was so simple, and I hadn't see that class :$ I shy. Thanks a lot anyway !!! :)

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.