Value of an attribute "value" in the <input type="checkbox"...>

Welcome to the Get Help category!


I would appreciate some help with the exercise below, as I didn’t get an understanding
of the concept. It concerns the checkbox input on page 7 of lesson 4 on HTML:

I can’t understand the whole idea behind setting a value of an attribute “value”.
On the page it doesn’t actually explain WHY you do it.

Could anybody please help? Very much appreciated.

Hi there,

So, if I understand your question correctly–
When an input is sent to a server via POST, the server receives the name (if given) and the value. This example should help you understand:

<form method="POST" action="server">
    <input type="checkbox" name="topping" value="lettuce">

If the checkbox for lettuce is selected and then submitted, the server will receive “name” (topping) with the value of “value” (lettuce)

That should be true, yes.
But the value of the attribute “value” is set BEFORE the user’s choice.
So, even if the user didn’t check the box “Lettuce”, the value is still true.
Isn’t that right?

The standard behavior for a checkbox is that it only sends its value to the server if it is checked.

Consider it like pressing an “on” button. Only the buttons that are “on” will be sent to the server and the buttons that are “off” are not–but, you still want the “off” buttons to have a value for when they are inevitably switched “on”

Does that help you out? Or are you wondering something else?

Oh, I didn’t consider that behaviour of the checkbox.
Thanks a lot, that does it.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.