javascript - Bind to click-event to dynamically created button without selector -


i'm trying dynamically create elements using javascript , jquery. i'm stuck binding simple click-event dynamically created button-element.

the button created with

var thebutton = $("<button>...</button>"); 

now want bind click-event, added

thebutton.on("click", function(){...}); // not working 

other solutions pointed out, must bound container. ended with

$(document).on("click", thebutton, function(){...}); // fires no matter click 

the problem selector (2nd param of on method) jquery-object @ point.

also tried

thebutton[0].onclick = () => alert("clicked") 

which not working

try this. it's working

$(function(){    var thebutton = $("<button>button</button>");    thebutton.click(function(){       alert('clicked');    })    $('body').append(thebutton); }) 

working fiddle


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -