Why is the value attribute excluded when making a drop-down menu's answer tag?

Viewed 13

I'm learning web development for fun.

Now I am making a drop-down menu that asks security questions. I learned how to create one from a tutorial online. What I'm still confused about is why the value attribute is excluded for the answer's input tag. I thought it would be needed to send the answer to the server? Does the name attribute allow the answer to be sent?

My code is below.

Thanks in advance!

    <label for="question">Security question</label>
    <select name="question" id="question">
        <option value="q1">What is your favorite Pokemon?</option>
        <option value="q2">What is your favorite book?</option>
        <option value="q3">What is your city of birth?</option>

    </select>

    <br><br>
    <label for="answer">Security question answer:</label>
    <input type="text" id="answer" name="answer">
1 Answers

Inputs of type "text" start out with a value of "" (empty string) by default (unless you set a different default with the defaultValue attribute), and then the value is whatever the user types.

You can set the value attribute your code to pre-fill the input, but it's generally not necessary. (You can also add a placeholder, which shouldn't affect the value, and disappears when the user starts typing into that input since it only shows up when input is empty (value = "").

And yes, the name attribute is the main requirement for submitting to server. Inputs generally have a default value. Even for drop-downs, default value is usually the first option ("q1" in your case), unless the selected attribute is added to one of the other options.

Related