Reputation: 9152
I have have the following
<div id="dualList"></div>
And I have written a plugin but for the sake of testing I have stripped it away. I am having problems with the plugin displaying the id of the div.
<script>
(function($) {
$.fn.DualList = function() {
var thisControl = $(this);
alert(thisControl.attr('id'));
}
})(jQuery);
</script>
And its bound on document on ready using $("#dualList").DualList();
Any ideas why the ID isnt echoing out?
Upvotes: 0
Views: 1864
Reputation: 2335
Widget is a collection wherever invoked, so
$(this.element[0]).attr("id")
will get you the id of the first element and so on..
Upvotes: 0
Reputation: 237875
First off, as far as I can tell, it does work.
However, there are a couple of things that are not optimal in your code. You can't be sure that there's just one onject selected (it could be a class selector, for all you know). You should therefore iterate through all the members of the selection. Second, you don't need the jQuery constructor to get the id
property. You can do it just with this.id
:
$.fn.DualList = function() {
return this.each(function() {
alert (this.id);
});
};
Working example of this style of code
Upvotes: 1