often, we need to create this when we do any projects there we mostly use JavaScript or jQuery for creating Html Tag or When we create Forms.
Dynamic drop-down list option with JavaScript
<button id ='drop_down'>Create Drop Down List</button>
\<br><br><br>
<div id="show_dropDown"></div>
- If we have data in an array then first we will store it in a variable.
- Get the id of Button using document.getElementById("Id here")
- Fire on click event on the button click.
- Create a select tag and add attribute name for it.
- Iterate loop of the array which we want to show in the dropdown list.
- Create an option tag.
- Add attribute value and assign the value of the array.
- Add attribute text and assign the value for array.
- Add option tag in the select tag.
- Get the id of div tag where we want to show a drop-down list.
- Append the select drop-down list on that div.
document.getElementById('drop_down').onclick = function (){
var arr = ['Ram','Zack','Mohan','Stevis'];
var select_drop_down = document.createElement('select');
select_drop_down.name = "dropdown";
for(var i=0;i<arr.length;i++ ){
var option = document.createElement('option');
option.value = i;
option.text = arr[i];
select_drop_down.add(option);
var show_dropDown = document.getElementById('show_dropDown');
show_dropDown.appendChild(select_drop_down);
}
}
JavaScript Code with Object Data
document.getElementById('drop_down').onclick = function (){
var obj = {
Name: "Zack", class: "10th", Address: "California"
};
var select_drop_down = document.createElement('select');
select_drop_down.name = "dropdown";
Object.keys(obj).forEach(key => {
var option = document.createElement('option');
option.value = key;
option.text = obj[key];
select_drop_down.add(option);
var show_dropDown = document.getElementById('show_dropDown');
show_dropDown.appendChild(select_drop_down);
});
}
document.getElementById('drop_down').onclick = function (){
var obj = {
Name: "Zack", class: "10th", Address: "California"
};
var select_drop_down = document.createElement('select');
select_drop_down.name = "dropdown";
Object.keys(obj).forEach(key => {
var option = document.createElement('option');
option.value = key;
option.text = obj[key];
select_drop_down.add(option);
var show_dropDown = document.getElementById('show_dropDown');
show_dropDown.appendChild(select_drop_down);
});
}
Output - Video
Dynamic drop-down list option with JQuery
<button id ='drop_down'>Create Drop Down List</button>
\<br><br><br>
<div id="show_dropDown"></div >
JQuery Code with Array Data
- If we have data in an array then first we will store it in a variable.
- Get the id of Button.
- Fire on click event on the button click.
- Create a select tag and add attribute name for it.
- Iterate loop of the array which we want to show in the dropdown list.
- Create an option tag.
- Add attribute value and assign the value of the array.
- Add attribute text and assign the value for array.
- Add dropdown html inside div tag using Id
- close the select tag.
var arr = ['Ram','Zack','Mohan','Stevis'];
var drop_down_html = '';
$(document).ready(function () {
$("#drop_down").on("click",function () {
drop_down_html += "<select name='dropdown'>" +
"<option val=''>Select Name</option>";
$.each(arr, function (index, value) {
drop_down_html += "<option value='"+index+"'>"+value+</option>";
$("#show_dropDown").html(drop_down_html);
});
drop_down_html += "</select>";
});
});
Output - Video
JQuery Code with Object Data
var drop_down_html ='';
var obj = {
Name: "Zack", class: "10th", Address: "California"
};
$("#drop_down").on("click",function () {
drop_down_html += "<select name='dropdown'>" +
"<option val=''>Select Name</option>";
$.each(obj, function (index, value) {
drop_down_html += "<option value='"+index+"'>"+value+</option>";
$("#show_dropDown").html(drop_down_html);
});
drop_down_html += "</select>";
});
Output - Video
If any mistakes I made here, so please let me know and improve me.If you have any query, feel free to ask me ЁЯШКRecommended post
- How to Encode and Decode Strings with Base64 in JavaScript examples
- How to shift button in Clock wise in javaScript using ECMA6?
- How to create QR code Genereator using Jquery
- How to Reset DropDownList Selection Using jQuery?
- Create dynamic drop-down list option with JavaScript and jQuery
- Onclick show and hide div using JQuery with examples
- How to Get All Google play store apps List using PHP Scraper?
- How to pass a variable from the command line using PHP?
- How to Submit Registration and Login Form in PHP?
- Introduction PHP
- how to declare variables in PHP?
- What things you need to install PHP?
- How to get meta tags in 2 easy way in PHP
Post a Comment
Please do not enter any spam link in the comment section.