Below is part of a plugin I'm working. Its my first plugin. I can access and send values to functions. But now when i try to get an array from a function, its sending me an Object! Can anyone tell me what is wrong with getValue function? its returning object instead of the array $selected_items
(function($){
function WMPlugin(item, options) {
this.options = $.extend({
selectableList_header: null,
selectedList_header: null,
}, options);
this.$element = $(item);
this.$container = $('<div/>', { 'class': "wmdl-container" });
this.$selectable_list = $('<ul/>', { 'class': 'wmdl-selectable-list' });
this.$selected_list = $('<ul/>', { 'class': 'wmdl-selected-list' });
this.config = $.extend(true, {
selectableList_header: null,
selectedList_header: null,
}, options || {});
this.init();
}
WMPlugin.prototype = {
init: function (options) {
$.extend(this.options, options);
var $select = this.$element;
var $wmdl = this.generateWMDL();
$select.after($wmdl);
$select.hide();
return $wmdl;
},
generateWMDL: function (options) {
var $container = this.$container;
var $selectable_list = this.selectableList();
var $selected_list = this.selectedList();
$container.append($selectable_list).append($selected_list);
return $container;
},
selectableList: function (options) {
$.extend(this.options, options);
var $this = this;
var $selectable_list = this.$selectable_list;
var $select = this.$element;
var $optgroups = $select.children('optgroup');
var $i = 1;
if( $optgroups.length > 0 ) {
$optgroups.each(function(i, group){
var $group = $(group);
var $group_label = $group.attr('label');
if ($group_label == "") {
$group_label = '<small>No name found</small>';
}
var $group_parent = $('<li>')
.addClass('wmdl-has-group')
.html('<span>' + $group_label + '</span>');
var $group_ul = $('<ul>')
.addClass('wmdl-group')
.attr('data-wmdl-group', 'wmdl-group-'+i);
$group.attr('id', 'wmdl-group-'+i);
$group_parent.append($group_ul);
$selectable_list.append($group_parent);
});
}
$select.find('option').each(function(i, item) {
var $item = $(item);
var $item_value = $(item).attr('value');
var $item_text = $(item).text();
var $item_optgroup = $item.parent('optgroup');
if ($item.is(':selected')) {
var $li = $('<li>')
.css('display', 'none')
.attr('data-wmdl-value', $item_value)
.text($item_text);
} else {
var $li = $('<li>')
.attr('data-wmdl-value', $item_value)
.text($item_text);
}
if ($item_optgroup.length > 0) {
var $item_optgroup_id = $item_optgroup.attr('id');
$selectable_list.find('[data-wmdl-group="'+ $item_optgroup_id +'"]').append($li);
} else {
$selectable_list.append($li);
}
$this.addItem($li);
});
return $selectable_list;
},
selectedList: function (options) {
$.extend(this.options, options);
var $this = this;
var $select = this.$element;
var $selected_list = this.$selected_list;
$select.find('option:selected').each(function(i, item) {
var $item = $(item);
var $item_value = $(item).attr('value');
var $item_text = $(item).text();
var $item_optgroup = $item.parent('optgroup');
if ($item_optgroup_label == "") {
$item_optgroup_label = '<small>No name found</small>';
}
var $li = $('<li>')
.attr('data-wmdl-value', $item_value)
.text($item_text);
if ($item_optgroup.length > 0) {
var $item_optgroup_id = $item_optgroup.attr('id');
var $item_optgroup_selectedList = $selected_list.find('[data-wmdl-group="'+ $item_optgroup_id +'"]');
if ($item_optgroup_selectedList.length > 0) {
$item_optgroup_selectedList.append($li);
} else {
var $item_optgroup_label = $item_optgroup.attr('label');
var $group_parent = $('<li>')
.addClass('wmdl-has-group')
.html('<span>' + $item_optgroup_label + '</span>');
var $group_ul = $('<ul>')
.addClass('wmdl-group')
.attr('data-wmdl-group', $item_optgroup_id);
$group_parent.append($group_ul);
$group_ul.append($li);
$selected_list.append($group_parent);
}
} else {
$selected_list.append($li);
}
$this.removeItem($li);
});
return $selected_list;
},
getValue: function () {
var $this = this;
var $selected_list = this.$selected_list;
var $selectable_list = this.$selectable_list;
var $selected_items = [];
$selected_list.find('li').each(function(){
var $value = $(this).attr('data-wmdl-value');
$selected_items.push($value);
})
return $selected_items;
},
}
// jQuery plugin interface
$.fn.WMDuelList = function(opt) {
var args = Array.prototype.slice.call(arguments, 1);
return this.each(function() {
var item = $(this), instance = item.data('WMPlugin');
if(!instance) {
// create plugin instance if not created
item.data('WMPlugin', new WMPlugin(this, opt));
} else {
// otherwise check arguments for method call
if(typeof opt === 'string') {
instance[opt].apply(instance, args);
}
}
});
}
}(jQuery));
Working JSFiddle - https://jsfiddle.net/uh7w0dwb/
thisis being called? have you tried diong aconsole.log($selected_items[1]);before the return to check if there's a second array item? orconsole.log($selected_items.length);$(element).MWDuelList('getValue);, i'm getting an object