0

I am new to Javascript and learning new things day by day. Here I need to click the button and new button are created now I need to again click that newly created button and create new button again and so on. It must be in Pure Javascript.Please help me out

 document.getElementById("btn").addEventListener("click",function(e) {
        var btn=document.createElement("BUTTON");
        var t=document.createTextNode("Click Me");
        //Some code to click dynamically created element
        btn.appendChild(t);
        document.body.appendChild(btn);
Isabel Inc
  • 1,871
  • 2
  • 21
  • 28
Tripti Pant
  • 530
  • 5
  • 6
  • so you need to add a click event. If you make it a generic function, than it is easier to reuse it. Or you can learn about event delegation. – epascarello Aug 08 '16 at 16:00
  • @epascarello yes I need to click dynamically created elememt.But i dont know how to manipulate it .How does event delegation work with dynamically created element.Please help – Tripti Pant Aug 08 '16 at 16:02
  • 1
    So did the answers provide any help? – epascarello Aug 08 '16 at 17:02

4 Answers4

0

Create a function to create a button that creates a button. Note: you're not appending the text node to the button.

If you want to watch changes in the DOM and add events to the buttons in a alternative way, check the answers in this question...

var body = document.getElementsByTagName('body')[0];

(function myButton() {

    var btn = document.createElement("BUTTON");
    var text = document.createTextNode("Click Me");

    // append the text to the button
    btn.appendChild(text);

    // append the btn to the body tag
    body.appendChild(btn);

    // adds the click event to the btn
    btn.addEventListener("click", myButton);

})();
Community
  • 1
  • 1
0

In this case, jQuery do the good jobs for you.

$(function($){
  $(document).on('click','button',function(e){
    // do your stuff.
  })
})

Here is another good answer using jQuery:

Event binding on dynamically created elements?

Community
  • 1
  • 1
Radian Jheng
  • 667
  • 9
  • 20
-1

Simply add a eventlistener to document, then check tag. You can further expland it by also adding a Id or Class to the buttons and check that aswell (in case you need multiple buttons that does different things)

document.addEventListener('click', function(event) {
    var clickedEl = event.target;
    if(clickedEl.tagName === 'BUTTON') {
      clickedEl.innerHTML = "clicked!";
        
        var btn=document.createElement("BUTTON");
        var t=document.createTextNode("Click Me");
   btn.appendChild(t); 
        document.body.appendChild(btn);
    }
});
<button>Click Me</button>
user2267175
  • 595
  • 5
  • 14
-1

Make it a generic function and bind the click events to that method.

function addButton () {
    var btn = document.createElement("BUTTON");
    btn.type = "button";
    var t = document.createTextNode("Click Me");
    btn.appendChild(t); 
    btn.addEventListener("click", addButton);
    document.body.appendChild(btn);
}

document.getElementById("btn").addEventListener("click", addButton);
<button type="button" id="btn">Button</button>

Or event delegation

function addButton () {
    var btn = document.createElement("BUTTON");
    btn.type = "button";
    var t = document.createTextNode("Click Me");
    btn.appendChild(t); 
    document.body.appendChild(btn);
}

document.body.addEventListener("click", function(e) {
    if (e.target.tagName==="BUTTON") {  //I personally would use class or data attribute instead of tagName
      addButton();
    }
});
<button type="button" id="btn">Button</button>
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • 1
    And the downvote reason? I would like to improve to make you happy. ;) – epascarello Aug 08 '16 at 16:14
  • +1, but note: `document.body.addEventListener("click"...` isn't good for performance as it'd check if certain element's tag name is equal to the number `476` everytime the page is clicked, and it's missing to uppercase `e.target.tagName`. –  Aug 08 '16 at 17:14