Skip to content

Instantly share code, notes, and snippets.

@natsu90
Last active December 16, 2020 16:01
Show Gist options
  • Save natsu90/8214903 to your computer and use it in GitHub Desktop.
Save natsu90/8214903 to your computer and use it in GitHub Desktop.
append 'Add New' button on select2
// append footer at the end of dropdown list
$('#select-id').select2('container').append('<hr style="margin:5px"><a href="javascript:void(0)" onclick="add_new_option()"><img src="images/plus.png"/> Add New</a>');
// trigger on click
function add_new_option() {
$('#select-id').select2('container').select2('close');
var new_option_val, new_option_name;
/*
display form/dialog box and add to db
*/
$('#select-id').append('<option value='+new_option_val+'>'+new_option_name+'</option>');
$('#select-id').select2('val', new_option_val);
}
}
@bhaireshm
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment