How to add options to the drop down using javascript

<!DOCTYPE html>
<html>
<head>
	<script>
		function myFunction()
		{   var nums = {4:"four",5:"five"};
		    for(var key in nums)
	     	{
				var x = document.getElementById("category");
	     		var option = document.createElement("option");
	     		option.text = nums[key];
		 		option.value = key;
	     		x.add(option);
			}
		}		
	</script>
</head>
<body>
	<select id="category">
	      <option value="1">one</option>
		  <option value="2">two</option>
		  <option value="3">three</option>
	  </select>
	<button onclick = "myFunction()">Click here to add</button>
	
</body>
</html>

Published by deeps blog

Programmer

Leave a comment

Design a site like this with WordPress.com
Get started