Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove option dynamically from jQuery Chosen

This works well to add an option dynamically to a jQuery chosen select box;

var select = $('select', editor.field('cms_module_system_tenancies.tenant_id').node() );
var newOption = $('<option value="'+tenant_id+'" selected>'+tenant_forename+' '+tenant_surname+'</option>');
select.append(newOption);
select.trigger("chosen:updated");

But, I can't figure out how to reverse the action and remove that newly added item the next time I trigger the select list.

Is there a reverse of select.append which would remove the option from the list?

like image 750
user1098178 Avatar asked Oct 31 '15 21:10

user1098178


2 Answers

Update jQuery Chosen Dynamically

Try this:

Remove all child nodes

$('#selectBox').empty().append('<option value="0">-- Select --</option>');  

Here we are first emptying the select box and then appending a default "Select" option.

Remove a single child node

$("#selectBox option[value='option1']").remove(); 

Trigger chosen:updated after empty() or remove()

$('#selectBox').trigger("chosen:updated"); 

Hope this is helpful.

like image 194
ArtisanBay Avatar answered Oct 18 '22 02:10

ArtisanBay


Check prepend this will insert first. reverse to append.

like image 36
Laurentiu Avatar answered Oct 18 '22 02:10

Laurentiu