Checkbox Dilemma

I am confused about how name attribute in checkboxes work, let’s take this example,

<input type="checkbox" name="travel">Mountains
<input type="checkbox" name="travel">Beaches
<input type="checkbox" name="travel">Adventure

Here, when user check two or more checkbox then in the data which we will receive, for different values we will get the same name. What I am thinking is when we use different names for different checkboxes then the data which we will receive will be more straightforward.

So, is it necessary to use the same name for different checkboxes in a group, or we can use separate names for each checkbox or is there any condition on which all this depends.

Thanks in advance :slight_smile:

Are you taking the freeCodeCamp course? If so, in the future
use the ‘Ask for Help’ button so we know what lesson you’re referencing.

Yes, reference this lesson

Maybe you’re confusing the name attribute and the value attribute.
You can reference this lesson

Yes. Think about how and why you would use checkboxes on a form as opposed to radio buttons. Checkboxes are all fields in a form that are associated with a specific question or information as in a multiple choice situation where more than one answer is acceptable. So they would be grouped together by same name.

You could then store their values (if checked) in an array and iterate through them to get the specific values.

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