Skip to content

Instantly share code, notes, and snippets.

@natsu90
Last active December 16, 2020 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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);
}
}
@anthonyattard
Copy link

Couldn't get this to work. Getting an "apply" error.

@copostic
Copy link

copostic commented Oct 8, 2018

Couldn't get this to work. Getting an "apply" error.

This is because of the .select2('container'), try add containerCssClass option on init and select the container with the specified class

@bhaireshm
Copy link

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