Created
March 28, 2013 20:55
-
-
Save glompix/5266719 to your computer and use it in GitHub Desktop.
Simple comboboxes (manually editable selects) for jQuery/Bootstrap. Quick and dirty.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.fn.editableSelect = function () { | |
var selects = $(this).filter('select'); | |
selects.each(function () { | |
// Add an edit button to the select control, bootstrap style. | |
var select = $(this); | |
select.wrap('<div class="input-append" />'); | |
var editButton = $('<button class="btn" type="button" title="Enter your own value"><i class="icon-edit"></i></button>'); | |
select.parent().append(editButton); | |
editButton.click(function () { | |
// Replace the select element with a textbox. Replace the edit button with a button that | |
// takes the user back to the select. (original select is retained via closure) | |
var wrapper = $(this).parent(); | |
wrapper.find('select, button').detach(); | |
var textbox = $('<input type="text" />').val(select.val()); | |
$.each(__editableSelectCopiedAttrs, function (index, value) { | |
textbox.attr(value, select.attr(value)); | |
}); | |
wrapper.append(textbox); | |
var revertButton = $('<button class="btn" type="button" title="Go back to select a value"><i class="icon-list"></i></button>'); | |
wrapper.append(revertButton); | |
revertButton.click(function () { | |
wrapper.find('input, button').remove(); | |
wrapper.append(select); | |
wrapper.append(editButton); | |
}); | |
}); | |
}); | |
}; | |
var __editableSelectCopiedAttrs = ['id', 'name', 'class']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment