Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $.fn.odropdown = function(options) {
- var default_settings = {
- placeholder : 'span',
- placeholder_text : '',
- selected_index : 0
- };
- var settings = $.extend( {}, default_settings, options );
- return this.each(function() {
- var $this = $(this);
- var $list = $this.find('ul');
- var selected_index = settings.selected_index;
- var $placeholder = $this.find(settings.placeholder);
- if (settings.placeholder_text.length) {
- $placeholder.text(settings.placeholder_text);
- }
- // Prepare Form / input selection
- var $input = $('input[name="' + $list.attr('data-target') + '"]');
- var $form = $this.parents('form');
- // Check if the form exist
- if ($form.length) {
- if (!$input.length) {
- $input = $('<input type="hidden" name="' + $list.attr('data-target') + '" value="" />');
- $input.appendTo($form);
- }
- }
- $this.on('click', function(event){
- $(this).toggleClass('active');
- return false;
- });
- $list.find('li').on('click',function(e){
- var $li = $(this);
- selected_index = $li.index();
- $placeholder.text($li.text());
- if ($input) {
- $input.attr('value', $li.attr('data-id'));
- // Trigger some events
- $input.trigger('change');
- $input.trigger('click');
- // Turn back off list
- $this.toggleClass('active');
- }
- });
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement