Newly added item to chosen.js make it selected automatically

39 views Asked by At

I have a text field and a button, the user can enter a name in the text box and press the button.

Currently as it stands my code adds the item to the options which is then available for the user to select.

$("#btnAddCast").click(function () {
   $('#listActivities').append('<option>'+ $("#txtCast").val() + '</option>');
   $('#listActivities').trigger('chosen:updated');
   return false;
});

What I want to do / can't find within the chosen.js documentation is how to make that newly added item selected automatically does anyone know how to do such a thing?

1

There are 1 answers

1
depperm On

It looks like the selects still act as normal selects. I'd just select the last item like so:

$("#btnAddCast").click(function () {
    $('#listActivities').append('<option>'+ $("#txtCast").val()+ '</option>');
    $('#listActivities :last').attr('selected','selected'); 
    return false;
});