Issue
In HTML, what is the difference of using the <select> tag with child <option>s inside versus a <button> with a <div> that has many children buttons that only appear and disappear when the button is pressed?
Smth like (dont mind that the options are different, concept is key):
<select name="dog-names" id="dog-names">
<option value="rigatoni">Rigatoni</option>
<option value="dave">Dave</option
<option value="pumpernickel">Pumpernickel</option>
</select>
versus smth like code this:
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
<h2>Clickable Dropdown</h2>
<p>Click on the button to open the dropdown menu.</p>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
Solution
The difference is that <select> tags are most useful when used in a <form> tag (ie: data is being sent to a server). In a JS-only app you can use <select>, but it just makes more sense to make a custom dropdown. This way, you can customize it however you want and add event listeners.
For example, if you had a web form that was sent data to a backend, then you would have to use an <select> tag like this:
<form action="/name.php" method="post">
What is your name?
<select name="dogname">
<option value="Bob">Bob is my name!</option>
<option value="Joe">Joe is my name!</option>
<option value="Jeff">Jeff is my name!</option>
</select>
<input type="submit" value="Submit">
</form>
In this example, the value attribute of the selected <option> tag would be sent in a parameter named dogname to the server. For a HTML form, this is exactly what we want.
However, it is hard to customize the look of this form and it is not very useful for elements like navbars. If you want to interact with the dropdown in JavaScript and style it with CSS, then it is best to use a <div> and <button> elements. Like this:
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.dropbtn {
background-color: #3498DB;
color: white;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
}
.dropbtn:hover, .dropbtn:focus {
background-color: #2980B9;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 160px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown a:hover {background-color: #ddd;}
.show {display: block;}
<h2>Clickable Dropdown</h2>
<p>Click on the button to open the dropdown menu.</p>
<div class="dropdown">
<button onclick="myFunction()" class="dropbtn">Dropdown</button>
<div id="myDropdown" class="dropdown-content">
<a href="#home">Home</a>
<a href="#about">About</a>
<a href="#contact">Contact</a>
</div>
</div>
As you see in the snippet above, the dropdown looks much better and is better suited for non-form elements. But, the selected <button> element isn't sent to any server like in the first snippet. For a navbar, this is exactly what we want.
Answered By - Michael M.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.