Add remove select and set items in select drop down list

Introduction:
In my previous article I have explained aboutConvert string to lowercase-uppercase in xslt. In this article I am going to explain about how to add,remove,check and set the items in select using jQuery
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’, true, true);
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();
Do you like this article? Help us to improve. Please post your comments below.

Comments

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!