harshit verma

harshit verma

  • NA
  • 36
  • 2.3k

Adding a value to a dropdown through Javascript in c#

Aug 13 2018 9:17 AM
Hi,
 
I want to add a value to the Dropdownlist through Javascript but am getting Runtime Errors.
 
Below is my code, can some one help me on this. I tried multiple approches from webSearch but are not working.
 
function run() {
debugger;
var ddlvalue = document.getElementById("<%=ddlTest.ClientID%>");
if (ddlvalue.options[ddlvalue.selectedIndex].value == "0")
{
alert("Please select a value from the Dropdown!");
return;
}
var option = document.createElement('option');
option.text = 'UK';
option.value = '5';
ddlvalue.add(option); //Tried no error but value not getting added to Dropdown list.
document.getElementById("<%=ddlTest.ClientID%>").add(option);//Tried giving runtime error.

Answers (5)