How to add default value to dropdown HTML (I did not know)

Is this what you’re trying to create in your HTML form?

how to add default value to dropdown HTML

If you’re looking for the answer, skip straight to the section below. I usually do a bit of a preamble in these posts, since this is ultimately a blog.

So, I’m currently teaching a front-end web development course at General Assembly, and one of the best things about being an instructor is that you get asked questions all the time.

Broad questions, specific questions, out-of-left-field questions. Every class has students asking them, and it’s awesome!

Sometimes, a student’s question is something you’d never thought of, or it’s related to something that you’ve just never had the chance to use at work or on side projects. Those are arguably the best opportunities for both the student and teacher to grow, and I relish them as they appear.

The great thing about very specific questions is that I’d have to google for the answer in front of the students. This indirectly demonstrates and hopefully imparts the meta-skill of googling for esoteric programming topics. That, and I get to learn something new too.

What’s not to like?

Ok, on to the answer that you’re probably here for.

Adding default value to dropdown HTML

Let’s get one thing straight: this is not obvious at all. So if you’re feeling at all embarrassed that you don’t know it already, don’t. I had to google for this answer in front of my class because I didn’t know it either!

Here is HTML for a normal <select> dropdown:

The above would render a dropdown with a default value pre-selected to be the first item in the list. So if the user hits “Submit” in the form, the value for the “course” field would default to “web-dev”. That’s no good!

Here’s the HTML you should add to give it a default value, which functions sort of like a placeholder:

Resulting HTML that has a default value:

Totally non-obvious answer. Hope it helped.

Happy developing!

Implementing a Queue in JavaScript?

Yesterday I asked myself whether it was necessary to implement a stack abstract data type in JavaScript. The answer turned out to be “no,” because you can just use the built-in Array data type to simulate a stack without obvious performance penalties.

Naturally, I started asking myself, what about a queue? Should we be implementing a queue in JavaScript?

Continue reading “Implementing a Queue in JavaScript?”

Why implement Stack in JavaScript when you have Array?

This is the stupidly simple question I asked myself today as I was trying to familiarise myself with data structures. Is there ever a need to implement a stack data structure in JavaScript when the built-in Array data structure already exists?

Based on my short research, the answer is no – there’s probably no need to implement a separate Stack constructor function or class in JavaScript. Since Array already has the typical methods you’d need for a stack to work, like push() and pop(), you can just use an array to simulate a stack.

Continue reading “Why implement Stack in JavaScript when you have Array?”