How to Set Default Value in Dropdownlist in HTML Using JavaScript

Selecting a default value in a dropdown list is very easy. Here we are going to learn how to select default value in a dropdown list using JavaScript for dynamic content.

Default Value in Dropdownlist Using JavaScript

Generally, while using various APIs and in some complex forms, we need to dynamically select the dropdown list.

The given below JavaScript code will help us to understand the process of selecting default value in a dropdown list dynamically.

		<title>errorsea - Dynamic Dropdown Select Option</title>
		<select id="my_menu"></select>
			var values = ["PHP", "JavaScript", "HTML", "CSS"];
			function select_default(my_option, all_options, dropdown_id){
				var temp = "";
				for(var i = 0; i < all_options.length; i++){
					if(my_option == all_options[i]){
						temp += "<option value='"+all_options[i]+"' selected>"+all_options[i]+"</option>";
						temp += "<option value='"+all_options[i]+"'>"+all_options[i]+"</option>";
				document.getElementById(dropdown_id).innerHTML = temp;
			select_default("HTML", values, "my_menu");


  • Here, we have created a JavaScript function named select_default()
  • In this function, we have passed three parameters. my_option, all_options, and dropdown_id
  • my_option is the value that we want to be selected as default.
  • all_options is the array of all available options for the dropdown list.
  • my_menu is the id of our select option where we want to select the default value.


I hope now you have a complete understanding of dropdown selection using JavaScript. We can do some more modifications to the above function for some more advancements.

Happy Scripting 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
add columns in table laravel
How to Add New Table Fields in Laravel

In laravel has models to control data tables and migrations to create and manage models. As we use migrations to...