2

I am using a asp drop down list. According to my requirement I need to set the values of that drop down list through external java script file. How is it possible? I will not use document object to get the drop down Id.

5
  • what do you mean by asp drop down list? are you using asp.net mvc? Commented Jun 1, 2012 at 10:49
  • Remember that in all likelihood, ASP.NET will not detect the new options correctly on a postback – listControl.Items should only contain items you add in code-behind or that are restored from ViewState. You'll have to figure out which items are selected some other way. (E.g. directly from the HTTP request.) Commented Jun 1, 2012 at 11:00
  • 1
    Also, what do you mean by "I will not use document object to get the drop down Id."? And are you using jQuery or not? That is, is it okay for the answer to be jQuery based or would you prefer plain Javascript? Commented Jun 1, 2012 at 11:01
  • @Inerdial It is OK for me to use the J query code to implement the above functionality. Please provide me the sample code snippet.That will help me a lot. Thanks in advance. Commented Jun 1, 2012 at 12:25
  • @SandeepGB Thanks for your response. I am using C# language for coding and in the page I am using aspx controls. Please provide the sample code. Commented Jun 1, 2012 at 12:27

1 Answer 1

2

If you can set up the list of option values as a js array then you can use jQuery's $.each method to build the dropdown like this

HTML

​<select id="drop"></select>​

JQuery

var values = ['test1','test2','test3','test4','test5','test6'​];

​$.each(values,function(i,val){
   $('<option />').text(val).val(val).appendTo('#drop');
});​

Working Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. But i am using ASP dropdown list. Please provide me the code snippet for the corresponding. Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.