Tuesday, March 15, 2011

Insert item in listbox at top using javascript

many of times we have requirement to add a item at top of listbox using javascript (add item at index 0).


       var txtl = document.getElementById("<%= txtvalue.ClientID %>");
        var lst = document.getElementById("<%=mylistbox.ClientID %>");
        if(txtl.value != "")
        {
            var opt = document.createElement("Option");
            opt.text =txtl.value;
            opt.value = txtl.value;
            lst.options.add(opt,0);  // here while adding new item to listbox we can pass its index number. 
        }



Solution By: Rajesh Rolen





Share This!


No comments:

Powered By Blogger · Designed By Seo Blogger Templates