Tuesday, September 25, 2012

Select text, value, remove, selecteIndex on select element

1. Add options to the end of a select






2. Add options to the start of a select


3. Replace all the options with new options


4. Replace items at a certain index

$("#drpCountry option:eq(1)").replaceWith('');

$("#drpCountry option:eq(2)").replaceWith('');

5. Set the element at index 2 to be selected

$("#drpCountry option:eq(2)").attr("selected", "selected");

6. Set the selected element by text

$("#drpCountry").val("India").attr("selected", "selected");

7. Set the selected element by value


8. Remove an item at a particular index

$("#drpCountry option:eq(0)").remove();

9. Remove first item

$("#drpCountry option:first").remove();

10. Remove last item

$("#drpCountry option:last").remove();

11. Get the text of the selected item

var selectedtext=$("#drpCountry option:selected").text();

12. Get the value of the selected item

var selectedvalue=$("#drpCountry option:selected").val();

13. Get the index of the selected item

var selectedIndex=$("#drpCountry option").index($("#drpCountry option:selected"));

14. Alternative way to get the selected item

var selectedItem=$("#drpCountry option:selected").prevAll().size();

15. Insert an item in after a particular position

$("#drpCountry option:eq(0)").after("");

16. Insert an item in before a particular position

$("#drpCountry option:eq(3)").before("");

17. Getting values when item is selected

$("#drpCountry").change(function() {

var Selectedvalue=$(this).val();

var Selectedtext=$(this).children("option:selected").text();


1 comment :

  1. hi suresh,

    i want a javascript function code to repeat a click event at a specified intervel... can you help me??


AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath