jill1993 jill1993 - 16 days ago 5
Javascript Question

How do you dynamically display a button using JavaScript?

Below I have code where I am trying to dynamically create a button using Javascript (I don't want to just create a button with HTML) When I locally run the web page no button appears.

The buttonActionFunction is a separate function I have (that is fully working) where an image appears when the button is clicked.

Also how do I add to that code so that once the button is clicked that the button deactivates so they can't click it again?

<script>
function buttonFunction() {
var button= document.createElement("button");
button.appendChild(document.createTextNode("Click Me"));
button.onclick=buttonActionFunction();
document.getElementById("divId").appendChild(button);
}
buttonFunction();
</script>

Answer
 function createButton(context, func) {
     var button = document.createElement("input");
     button.type = "button";
     button.value = "Click Me";
     button.onclick = func;
     context.appendChild(button);
 }
 function functienaam()
 {
     createButton(document.body, functienaam);
 }
 createButton(document.body, functienaam);

jsfiddle : https://jsfiddle.net/fv42orad/1/

Comments