HTML legend element is not counted in form

I have the following HTML:

<!DOCTYPE html> <html> <head> <title>JavaScript &amp; jQuery - Chapter 13: Form Enhancement and Validation - Select All Checkboxes</title> <link rel="stylesheet" href="css/c13.css" /> </head> <body> <div class="container login"> <form id="interests" action="/login" method="post"> <div class="one-third column"> <img src="img/logo.png" alt="logo" id="logo" /> </div> <div class="two-thirds column" id="main"> <fieldset> <legend>Genres</legend> <label><input type="checkbox" value="all" id="all">All</label> <label><input type="checkbox" name="genre" value="animation">Animation</label> <label><input type="checkbox" name="genre" value="docs">Documentary</label> <label><input type="checkbox" name="genre" value="shorts">Shorts</label> </fieldset> </div><!-- .two-thirds --> </form> </div><!-- .container --> <script src="js/utilities.js"></script> <script src="js/all-checkboxes.js"></script> </body> 

In the all-checkbox.js file, I have the following lines:

 ... var form = document.getElementById('interests'); var elements = form.elements; ... 

When I load the page and look at the elements variable in the debugger (Firefox), I see that it is an array of length 5. I understand that form.elements will return all form elements. The debugger displays the following values ​​for items:

 0: <fieldset> 1: <input#all> 2: <input> 3: <input> 4: <input> 

I thought that the element of legend was also an element of form, but obviously here it is not considered so.

Can someone enlighten me?

+8
html forms elements
source share
1 answer

getElementById returns an HTMLFormElement object. legend not a listed element in the elements Property.

Regarding why ... / shrug. I can't seem to find a backstory. If I were to guess, I would say because this is not the actual control for the form, and perhaps the forces that HTMLFORMElement.elements will feel mean "controls." But then again, just speculation.

+1
source share

All Articles