JavaScript click() for dynamically created button click event dynamically created button jquery,jquery click dynamically created

Your $.each loop runs before you add the buttons. So it does nothing.

Change

$.each($('[name^=mins]'), function (i, item) {
$(document).on('click', 'button', function(e) {
 alert("1234");
});
});

to

$(document).on('click', 'input[name^=mins]', function(e) {
  alert("1234");
});
Answer:1

Try this

$("button").click(function() {
    $("div").append("<input type='button' class='mins' value='button' name='mins[]' />")
});


$(document).on('click', '.mins', function(e) {
 alert("1234");
});

DEMO

Answer:2

change your code with this code

$("button").click(function() {
    $("div").append("<input type='button' class='mins' value='button' name='mins[]' />")
});


$(document).on('click', '.mins', function(e) {
 alert("1234");
});

Fiddle: http://jsfiddle.net/bnq3P/7/

Answer:3

It should be:

$(document).on('click', 'input:button[name^=mins]', function (e) {
    alert("1234");
});

Updated fiddle here.

Answer:4

Another way to do this is when you create your button give it a function

$("button").click(function () {
    var $input = $("<input>", {
        type: 'button',
        class: 'mins',
        value: 'button',
        name: 'mins[]',
        click: function () {
            alert("1234");
        }
    });
    $("div").append($input)
});

DEMO

Answer:5

You can change your javascript code with this code

$("button").click(function() {
    $("div").append("<input type='button' class='mins' value='button' name='mins[]' />");
$('.mins').off('click');
$('.mins').on('click', function(e) {
 alert("1234");
});
});

You can check this working demo here http://jsfiddle.net/bnq3P/14/

Answer:6

The Problem: The call back on my $.getJSON request doesn't run. On page load, nothing is logged to the console or updated on the page, but when the function is pasted in to the console it executes ...

The Problem: The call back on my $.getJSON request doesn't run. On page load, nothing is logged to the console or updated on the page, but when the function is pasted in to the console it executes ...

I need to pass a dynamic param to an applet. This is my controller: 'use strict'; angular.module('jworkApp') .controller('AppletCtrl',['$scope', function (scope) { scope....

I need to pass a dynamic param to an applet. This is my controller: 'use strict'; angular.module('jworkApp') .controller('AppletCtrl',['$scope', function (scope) { scope....

Using Fabric.js, I'm having trouble truly rotating a triangle around its center point, or at least what I believe should be the center point. I created a jsFiddle that demonstrates. The triangle is ...

Using Fabric.js, I'm having trouble truly rotating a triangle around its center point, or at least what I believe should be the center point. I created a jsFiddle that demonstrates. The triangle is ...

I have seen many answers in stack overflow which says setting response headers will make you "CORS" request.But no solution worked for me.I have written the following code: //Server.js Code var ...

I have seen many answers in stack overflow which says setting response headers will make you "CORS" request.But no solution worked for me.I have written the following code: //Server.js Code var ...

  1. making cors request with fetch
  2. making cors request in angularjs
  3. make cors request
  4. make cors request javascript
  5. make cors request ajax
  6. make cors request axios
  7. make cors request in postman
  8. make cors request online
  9. make cors request react
  10. make cors request from chrome
  11. making a cors request with jquery
  12. angular make cors request
  13. js make cors request
  14. curl make cors request
  15. vue make cors request
  16. reactjs make cors request