In the above example, we have shown how to use legend element with fieldset element. We have created a simple form having two fields. We have included the form in the fieldset element which gives it a better look. The legend element renders as label or header for fieldset element.

In the above example, on button click, we created <legend> element, created a text node and appended to <legend> element, further we appended <legend> element to <fieldset> along with label and textbox element. And finally, we appended the fieldset element to <div> element. Thus on button click we have created a small form having fieldset, legend and one field in it using javascript.