We can create a <legend> element via the document.createElement()
method:
var x = document.createElement("LEGEND");
Click the button to create a LEGEND element with some text.
<!DOCTYPE html> <html> <body> <fieldset id="myFieldset"> Name: <input type="text"> </fieldset><br> <button onclick="myFunction()">Test</button> <script> function myFunction() {//w ww .jav a 2s .c o m var x = document.createElement("LEGEND"); var t = document.createTextNode("MyNameFieldset:"); x.appendChild(t); document.getElementById("myFieldset").appendChild(x); } </script> </body> </html>