Given the select box below. I want to be able to dynamically add a new option.
<select id="user_department_id" name="user[department_id]">
<option value=""> </option>
<option value="9">AAAAA</option>
<option value="11">BBBBB</option>
<option value="10">G</option>
<option value="12">Z</option>
<option value="">--</option>
<option value="add">Add a New</option>
</select>
I have been using the following to add a new option:
$('#user_department_id')
.prepend($("<option></option>")
.attr("value",data.id)
.text(data.title)
);
The problem here is that it is position unnaturally, it's above the empty placeholder option and not sorted alphabetically. Is there a magic way to append a new select option in the correct place?
Thanks