Fire onclick event on the element created by javascript using element.innerHTML [duplicate]












0
















This question already has an answer here:




  • Click event doesn't work on dynamically generated elements [duplicate]

    20 answers




I'm creating a delete button by javascript using this code:



btnclick = '<button class="deleteMe">' + 'X' + '</button>';
output.innerHTML = "whatever" + btnclick;


How do I fire onclick function on this button? Right now I'm doing something like this:



$(document).ready(function() {
$(".deleteMe").on("click", function(){
alert("clicked");
});
});


And I'm not getting any alert message on button click.










share|improve this question















marked as duplicate by Rory McCrossan javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 8:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • This will helps you stackoverflow.com/questions/6658752/…

    – Hamza Haider
    Jan 2 at 8:01
















0
















This question already has an answer here:




  • Click event doesn't work on dynamically generated elements [duplicate]

    20 answers




I'm creating a delete button by javascript using this code:



btnclick = '<button class="deleteMe">' + 'X' + '</button>';
output.innerHTML = "whatever" + btnclick;


How do I fire onclick function on this button? Right now I'm doing something like this:



$(document).ready(function() {
$(".deleteMe").on("click", function(){
alert("clicked");
});
});


And I'm not getting any alert message on button click.










share|improve this question















marked as duplicate by Rory McCrossan javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 8:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • This will helps you stackoverflow.com/questions/6658752/…

    – Hamza Haider
    Jan 2 at 8:01














0












0








0









This question already has an answer here:




  • Click event doesn't work on dynamically generated elements [duplicate]

    20 answers




I'm creating a delete button by javascript using this code:



btnclick = '<button class="deleteMe">' + 'X' + '</button>';
output.innerHTML = "whatever" + btnclick;


How do I fire onclick function on this button? Right now I'm doing something like this:



$(document).ready(function() {
$(".deleteMe").on("click", function(){
alert("clicked");
});
});


And I'm not getting any alert message on button click.










share|improve this question

















This question already has an answer here:




  • Click event doesn't work on dynamically generated elements [duplicate]

    20 answers




I'm creating a delete button by javascript using this code:



btnclick = '<button class="deleteMe">' + 'X' + '</button>';
output.innerHTML = "whatever" + btnclick;


How do I fire onclick function on this button? Right now I'm doing something like this:



$(document).ready(function() {
$(".deleteMe").on("click", function(){
alert("clicked");
});
});


And I'm not getting any alert message on button click.





This question already has an answer here:




  • Click event doesn't work on dynamically generated elements [duplicate]

    20 answers








javascript jquery onclick innerhtml






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 8:06









Rory McCrossan

247k29213253




247k29213253










asked Jan 2 at 7:57









Ayushi SoodAyushi Sood

65




65




marked as duplicate by Rory McCrossan javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 8:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Rory McCrossan javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 8:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • This will helps you stackoverflow.com/questions/6658752/…

    – Hamza Haider
    Jan 2 at 8:01



















  • This will helps you stackoverflow.com/questions/6658752/…

    – Hamza Haider
    Jan 2 at 8:01

















This will helps you stackoverflow.com/questions/6658752/…

– Hamza Haider
Jan 2 at 8:01





This will helps you stackoverflow.com/questions/6658752/…

– Hamza Haider
Jan 2 at 8:01












1 Answer
1






active

oldest

votes


















0














suppose you have div as output,



you can simply append the button in your div and execute the function






$('#output').append('whatever  <button class="deleteMe">x</button>');
$(document).ready(function(){
$('body').on('click','.deleteMe',function(){
alert('delete button clicked!!!');
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output"></div>








share|improve this answer


























  • Thankyou, it helped me. But can you please tell me why did you use output.append and how did it make a difference And why was it not working while I was using output.innerHTML

    – Ayushi Sood
    Jan 2 at 8:46













  • .append is a Jquery function, I don't know what is output in your scenerio like (div, body, span) and if you want to use innerHTML here use like output[0].innerHTML

    – user6656728
    Jan 2 at 8:50













  • if you understood, do upvote my answer

    – user6656728
    Jan 2 at 10:05











  • Jquery event listeners don't work on dynamic elements. You can use below syntax for adding event listener to a dynamic element : $(document).ready(function(){ $("body").on("click",".deleteMe", function(){ alert("clicked"); }); });

    – AkKi
    Jan 2 at 10:07






  • 1





    This solution will not work. Please update your answer as follows : $('#output').append('whatever <button class="deleteMe">x</button>'); $(document).ready(function(){ $('body').on('click', '.deleteMe',function(){ alert('delete button clicked!!!'); }); });

    – AkKi
    Jan 2 at 15:25


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














suppose you have div as output,



you can simply append the button in your div and execute the function






$('#output').append('whatever  <button class="deleteMe">x</button>');
$(document).ready(function(){
$('body').on('click','.deleteMe',function(){
alert('delete button clicked!!!');
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output"></div>








share|improve this answer


























  • Thankyou, it helped me. But can you please tell me why did you use output.append and how did it make a difference And why was it not working while I was using output.innerHTML

    – Ayushi Sood
    Jan 2 at 8:46













  • .append is a Jquery function, I don't know what is output in your scenerio like (div, body, span) and if you want to use innerHTML here use like output[0].innerHTML

    – user6656728
    Jan 2 at 8:50













  • if you understood, do upvote my answer

    – user6656728
    Jan 2 at 10:05











  • Jquery event listeners don't work on dynamic elements. You can use below syntax for adding event listener to a dynamic element : $(document).ready(function(){ $("body").on("click",".deleteMe", function(){ alert("clicked"); }); });

    – AkKi
    Jan 2 at 10:07






  • 1





    This solution will not work. Please update your answer as follows : $('#output').append('whatever <button class="deleteMe">x</button>'); $(document).ready(function(){ $('body').on('click', '.deleteMe',function(){ alert('delete button clicked!!!'); }); });

    – AkKi
    Jan 2 at 15:25
















0














suppose you have div as output,



you can simply append the button in your div and execute the function






$('#output').append('whatever  <button class="deleteMe">x</button>');
$(document).ready(function(){
$('body').on('click','.deleteMe',function(){
alert('delete button clicked!!!');
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output"></div>








share|improve this answer


























  • Thankyou, it helped me. But can you please tell me why did you use output.append and how did it make a difference And why was it not working while I was using output.innerHTML

    – Ayushi Sood
    Jan 2 at 8:46













  • .append is a Jquery function, I don't know what is output in your scenerio like (div, body, span) and if you want to use innerHTML here use like output[0].innerHTML

    – user6656728
    Jan 2 at 8:50













  • if you understood, do upvote my answer

    – user6656728
    Jan 2 at 10:05











  • Jquery event listeners don't work on dynamic elements. You can use below syntax for adding event listener to a dynamic element : $(document).ready(function(){ $("body").on("click",".deleteMe", function(){ alert("clicked"); }); });

    – AkKi
    Jan 2 at 10:07






  • 1





    This solution will not work. Please update your answer as follows : $('#output').append('whatever <button class="deleteMe">x</button>'); $(document).ready(function(){ $('body').on('click', '.deleteMe',function(){ alert('delete button clicked!!!'); }); });

    – AkKi
    Jan 2 at 15:25














0












0








0







suppose you have div as output,



you can simply append the button in your div and execute the function






$('#output').append('whatever  <button class="deleteMe">x</button>');
$(document).ready(function(){
$('body').on('click','.deleteMe',function(){
alert('delete button clicked!!!');
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output"></div>








share|improve this answer















suppose you have div as output,



you can simply append the button in your div and execute the function






$('#output').append('whatever  <button class="deleteMe">x</button>');
$(document).ready(function(){
$('body').on('click','.deleteMe',function(){
alert('delete button clicked!!!');
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output"></div>








$('#output').append('whatever  <button class="deleteMe">x</button>');
$(document).ready(function(){
$('body').on('click','.deleteMe',function(){
alert('delete button clicked!!!');
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output"></div>





$('#output').append('whatever  <button class="deleteMe">x</button>');
$(document).ready(function(){
$('body').on('click','.deleteMe',function(){
alert('delete button clicked!!!');
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="output"></div>






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 4:30









AkKi

17915




17915










answered Jan 2 at 8:05







user6656728




















  • Thankyou, it helped me. But can you please tell me why did you use output.append and how did it make a difference And why was it not working while I was using output.innerHTML

    – Ayushi Sood
    Jan 2 at 8:46













  • .append is a Jquery function, I don't know what is output in your scenerio like (div, body, span) and if you want to use innerHTML here use like output[0].innerHTML

    – user6656728
    Jan 2 at 8:50













  • if you understood, do upvote my answer

    – user6656728
    Jan 2 at 10:05











  • Jquery event listeners don't work on dynamic elements. You can use below syntax for adding event listener to a dynamic element : $(document).ready(function(){ $("body").on("click",".deleteMe", function(){ alert("clicked"); }); });

    – AkKi
    Jan 2 at 10:07






  • 1





    This solution will not work. Please update your answer as follows : $('#output').append('whatever <button class="deleteMe">x</button>'); $(document).ready(function(){ $('body').on('click', '.deleteMe',function(){ alert('delete button clicked!!!'); }); });

    – AkKi
    Jan 2 at 15:25



















  • Thankyou, it helped me. But can you please tell me why did you use output.append and how did it make a difference And why was it not working while I was using output.innerHTML

    – Ayushi Sood
    Jan 2 at 8:46













  • .append is a Jquery function, I don't know what is output in your scenerio like (div, body, span) and if you want to use innerHTML here use like output[0].innerHTML

    – user6656728
    Jan 2 at 8:50













  • if you understood, do upvote my answer

    – user6656728
    Jan 2 at 10:05











  • Jquery event listeners don't work on dynamic elements. You can use below syntax for adding event listener to a dynamic element : $(document).ready(function(){ $("body").on("click",".deleteMe", function(){ alert("clicked"); }); });

    – AkKi
    Jan 2 at 10:07






  • 1





    This solution will not work. Please update your answer as follows : $('#output').append('whatever <button class="deleteMe">x</button>'); $(document).ready(function(){ $('body').on('click', '.deleteMe',function(){ alert('delete button clicked!!!'); }); });

    – AkKi
    Jan 2 at 15:25

















Thankyou, it helped me. But can you please tell me why did you use output.append and how did it make a difference And why was it not working while I was using output.innerHTML

– Ayushi Sood
Jan 2 at 8:46







Thankyou, it helped me. But can you please tell me why did you use output.append and how did it make a difference And why was it not working while I was using output.innerHTML

– Ayushi Sood
Jan 2 at 8:46















.append is a Jquery function, I don't know what is output in your scenerio like (div, body, span) and if you want to use innerHTML here use like output[0].innerHTML

– user6656728
Jan 2 at 8:50







.append is a Jquery function, I don't know what is output in your scenerio like (div, body, span) and if you want to use innerHTML here use like output[0].innerHTML

– user6656728
Jan 2 at 8:50















if you understood, do upvote my answer

– user6656728
Jan 2 at 10:05





if you understood, do upvote my answer

– user6656728
Jan 2 at 10:05













Jquery event listeners don't work on dynamic elements. You can use below syntax for adding event listener to a dynamic element : $(document).ready(function(){ $("body").on("click",".deleteMe", function(){ alert("clicked"); }); });

– AkKi
Jan 2 at 10:07





Jquery event listeners don't work on dynamic elements. You can use below syntax for adding event listener to a dynamic element : $(document).ready(function(){ $("body").on("click",".deleteMe", function(){ alert("clicked"); }); });

– AkKi
Jan 2 at 10:07




1




1





This solution will not work. Please update your answer as follows : $('#output').append('whatever <button class="deleteMe">x</button>'); $(document).ready(function(){ $('body').on('click', '.deleteMe',function(){ alert('delete button clicked!!!'); }); });

– AkKi
Jan 2 at 15:25





This solution will not work. Please update your answer as follows : $('#output').append('whatever <button class="deleteMe">x</button>'); $(document).ready(function(){ $('body').on('click', '.deleteMe',function(){ alert('delete button clicked!!!'); }); });

– AkKi
Jan 2 at 15:25





Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas