Create a Ul Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Ul

Introduction

You can create a <ul> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">create a UL and LI element</button>

<script>
function myFunction() {/*from  ww w.j a v a  2s.c om*/
    var x = document.createElement("UL");
    x.setAttribute("id", "myUL");
    document.body.appendChild(x);

    var y = document.createElement("LI");
    var t = document.createTextNode("Tea");
    y.appendChild(t);
    document.getElementById("myUL").appendChild(y);
}
</script>

</body>
</html>

Related Tutorials