0

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>
7
  • can you provide a fiddle or snippet ? Commented Nov 17, 2016 at 13:18
  • @PunitGajjar I don't understand what you mean, I'm new to Javascript! Commented Nov 17, 2016 at 13:19
  • 1
    jsfiddle.net provide working example and share here Commented Nov 17, 2016 at 13:20
  • thanks for sharing the link @lordkain Commented Nov 17, 2016 at 13:21
  • can you share you html code Commented Nov 17, 2016 at 13:22

3 Answers 3

2
 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/

Sign up to request clarification or add additional context in comments.

1 Comment

That is great thanks, I got it working! I just made some slight changes. Where you had functienaam I added my function that gave the action for the button and then at the botton when you have createButton I just removed the stuff in brackets and now it works perfectly. Thank so much for your help you absolute legend
0

Add <div id="divId"></div> to your html

As for disabling the button on click

   function buttonActionFunction(e) {
        e.target.disabled = true
    }

and button.onclick=buttonActionFunction; not button.onclick=buttonActionFunction();

2 Comments

to add to it, button.onclick=buttonActionFunction(); should rather be button.onclick=buttonActionFunction;
Then make sure that <div> is declared in html doc before the js code :)
0

Based on @lordKain answer I've updated the code to disable the previous button when it is clicked, as shown bellow:

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

https://jsfiddle.net/5unLc5xr/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.