Last active
August 29, 2015 13:57
-
-
Save jiminys/9524815 to your computer and use it in GitHub Desktop.
input-group; creates a radio-esque selector for text inputs, using a data- selector.
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
(function ($) { | |
function inputHandler(eventObject){ | |
var textbox = eventObject.target; | |
if (textbox.disabled === true) { | |
return; | |
} | |
if ($(textbox).val().length > 0) { | |
select(textbox); | |
} else { | |
enableAll($(textbox).attr('data-input-group')); | |
} | |
} | |
function enableAll (groupName) { | |
$("[data-input-group='" + groupName + "']").prop('disabled', false).removeClass('disabled'); | |
} | |
function disableAll (groupName) { | |
$("[data-input-group='" + groupName + "']").prop('disabled', true).addClass('disabled'); | |
} | |
function select(activeInput) { | |
var groupName = $(activeInput).attr("data-input-group"); | |
$("[data-input-group='" + groupName + "']").each(function(){ | |
if(this.id != $(activeInput).attr('id')){ | |
$(this).prop('disabled', true).addClass('disabled'); | |
$(this).val(''); | |
} else { | |
$(this).prop('disabled', false).removeClass('disabled'); | |
} | |
}); | |
} | |
function clearAll(groupName){ | |
$("[data-input-group='" + groupName + "']").val(''); | |
} | |
$('[data-input-group]').on('input', inputHandler); | |
var groups = []; | |
$('[data-input-group]').each(function(){ | |
var groupName = $(this).attr("data-input-group"); | |
if(groups.indexOf(groupName) > -1){ | |
return true; | |
} else { | |
groups.push(groupName); | |
} | |
$("[data-input-group='" + groupName + "']").each(function(){ | |
if($(this).val().length > 0){ | |
select(this); | |
return false; | |
} | |
return true; | |
}); | |
}); | |
var ig = {}; | |
ig.enableAll = enableAll; | |
ig.disableAll = disableAll; | |
ig.select = select; | |
ig.clearAll = clearAll; | |
window.inputGroup = ig; | |
return ig; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment