0

I try to insert a button with .html(...) and then he should execute some code. But it doesn´t work, the button (inserted with .html(...)) doesn´t response after clicking him.. i have no idea what i can do, i also asked google for help, but i found nothing.. i hope somebody can help me :)

Here is my html:

<html>
<head>
    <title>Multiplie Files</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="js/libs/jquery/jquery.js"></script>
    <script type="text/javascript" src="js/files.js"></script>
</head>
<body>
    <button id="button">test</button>

    <div id="field"></div>
</body>

And here is my javascript:

$('document').ready(function() {

$('#click').click(function() {
   alert(true); 
});

$('#button').click(function(){
    $('#field').html('<input value="Click" type="Button" id="click"/>');
    //document.getElementById("field").innerHTML = '<input value="Click" type="Button" id="click"/>';
});

});

I tried with jquery .html and with normal javascript .innerHTML.. nothing works..

Thanks for every reply! :)

Nico
  • 699
  • 1
  • 7
  • 16
  • You can also visit http://stackoverflow.com/questions/21239210/jquery-event-not-triggering-for-dom-elements-created-after-page-load/21239248#21239248 – Satpal Jun 19 '14 at 11:45
  • Seems like ur inserting the element with id=click after trying to look it up and assign a click handler. Try reversing the last two statements. – ctt Jun 19 '14 at 11:45

1 Answers1

1

Try to use event-delegation on dynamically created elements,

$('#field').on('click','#click',function() {
   alert(true); 
});
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • yeah thanks it works! could you be so nice and explain me this "event-delegation" in a simple and short way? :) – Nico Jun 19 '14 at 11:48
  • 1
    @Nico, Raja has already provided you the link. I don't want to be rude. Can't you follow that – Satpal Jun 19 '14 at 11:51
  • yes i saw it, but its not very easy for me to understand this.. i had thought he knows this and could simply explain me that, but then i try to study it by myself – Nico Jun 19 '14 at 11:54