Saturday, May 23, 2015

Add Remove Selected And Set Items In Select Drop Down List MVC

Explanation:
For example in my form I have a select option with id “seloption”. To add a item with value “option1” and with text “Option-1” below code is used.


var options = jQuery('#seloption').attr('options');          
options[options.length] = new Option('Option Name''option value'truetrue);

Now to remove the option with value “option1” below code is used.

jQuery("#seloption option[value=option1]").remove();

To check whether the option with value “option1” is present in the select below code is used

jQuery("#seloption option[value='option1']").length>0

If the option is present then it will return the index value of the option. Otherwise it will return -1. To make the option “option1” as a selected option below code is used.

jQuery("#seloption ").val("option1");

To get the selected value of the select below code is used.


var value = $('select#dropDownIdoption:selected').val();