Create an Anchor Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Anchor

Introduction

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {/*from   ww  w . j  a  v  a  2s  . com*/
    var x = document.createElement("A");
    var t = document.createTextNode("Tutorials");
    x.setAttribute("href", "https://www.java2s.com");
    x.appendChild(t);
    document.body.appendChild(x);
}
</script>

</body>
</html>

Related Tutorials