Last active
August 29, 2015 14:22
-
-
Save olimortimer/3c675a367b3e3e9f0103 to your computer and use it in GitHub Desktop.
JS: Pre-populate Fields
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
// Populate our field | |
function populateField($container, name, value) { | |
var $field = $container.find('[name="'+name+'"]'); | |
// Skip if our field doesn't exist | |
if($field.length == 0) return false; | |
if($field.is(':radio')) { | |
$field.filter('[value="'+value+'"]').prop('checked', true); | |
// Toggle our button state, if we're using a btn-group | |
$field.filter('[value="'+value+'"]').parent('label.btn').button('toggle'); | |
} else if($field.is('select')) { | |
$field.find('option[value="'+value+'"]').attr('selected', true); | |
// If select2, refresh it | |
if($field.hasClass('select2')) $field.select2(); | |
} else { | |
$field.val(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment