Create a Button Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Button

Introduction

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

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">create an Input Button</button>

<script>
function myFunction() {// w w w  .j ava 2  s  .co m
    var x = document.createElement("BUTTON");
    var t = document.createTextNode("this is the button");
    x.appendChild(t);
        

    document.body.appendChild(x);
}
</script>

</body>
</html>
===Title:Button value Property - Change the value of the value attribute of a button:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button id="myBtn" name="myname" value="myvalue" onclick="console.log(this.value)">My Button</button>

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

<script>
function myFunction() {//from   w  w  w  .  ja  v a 2  s.  c om
    document.getElementById("myBtn").value = "newButtonValue";
}
</script>

</body>
</html>

Related Tutorials