javascript - JQuery - Remove all option from Selectbox except for first and selected -


i have html select box several options. upon selection of option want selected option , first option shown in select option dropdown. know how filter , show 1 option cannot figure out how include both option 'selected' option remains 'selected'. have include snippets similar of code demo.

html:

<select id="mydropdown"> <option selected>select fruit</option> <option>grapes</option> <option>bananas</option> <option>oranges</option> <option>apples</option> </select> 

js:

$(document).on('change', '#mydropdown', function() {     $('option', this).not(this,'option:gt(0)').siblings().remove(); }); 

i trying use first option perform function 'on change' need selected option remain selected while rest being filtered out except first. example if 'oranges' selected, see:

<select id="mydropdown">      <option>select fruit</option>      <option>oranges</option>  //selcted value </select> 

btw using jquery mobile style well.

jsfiddle

.not takes 1 parameter. if want filter on 2 different selectors, can add comma between them inside string. can use :selected selector:

$('option', this).not(':eq(0), :selected').remove(); 

see updated fiddle: http://jsfiddle.net/znx9hxvu/9/


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -