javascript - Dynamically create element but use <a href> instead of a button onClick -


i intend re-use html dom's create element method described in w3schools form.

function myfunction() {    var btn = document.createelement("button");    var t = document.createtextnode("click me");    btn.appendchild(t);    document.body.appendchild(btn);  }
<p>click button make button element text.</p>    <button onclick="myfunction()">try it</button>

in example above, button used create html element. how can change use href link (an <a> tag) instead of button's click event?

change tag anchor, set href "javascript:void(0)" in order prevent browser perform navigation.

<!doctype html> <html> <body>  <p>click link make button element text.</p>  <a href="javascript:void(0)" onclick= "myfunction()" > try it</a>  <script> function myfunction() {             var btn = document.createelement("button");             var t = document.createtextnode("click me");             btn.appendchild(t);             document.body.appendchild(btn);         } </script>  </body> </html> 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -