I'm using jQuery and have some interactions with a jQuery UI where I need to get options. However there's a possibility that the jQuery UI function has not been applied yet to the DOM object. I'm getting a JavaScript error right now when I access an option.
I have a DOM object that has the progressbar (http://docs.jquery.com/UI/Progressbar) attached to it (maybe). In another thread, I'm trying to access the options using domObj.progressbar("option", "value")
.
How do I determine if that domObj
has a progressbar
attached?
As of jQuery UI 1.11, widgets can be directly tested for via the
instance
method:Documentation: http://api.jqueryui.com/jQuery.widget/#method-instance
I prefer to use hasOwnProperty on any object in JavaScript, since it returns a boolean every time.
Which IMHO is a little better than checking null; what if it switches to undefined, which, also IMO is what it should return.
You can access the progress bar object on the element by doing:
So to use this:
The DOM object will get an extra CSS class appended to it: "ui-progressbar". If you View Source on http://docs.jquery.com/UI/Progressbar then you can see a div with id=progressbar and nothing more, but if you use Firebug and click the element you can see it has a few more classed added.
Edited: I think that @jamiebarrow's solution is the more correct one here.
According to this discussion, the recommended way to detect if a widget has been rendered on an element is as follows: