Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

html button click event

I created a div as defined below

       <div class="area1">
        </div>

With this ajax call I changed the html for above div

$('#abutton').click(function(e) {
    e.preventDefault();
    $.ajax({
        url: 'do',
        type: 'POST',
        dataType: 'json',
        cache: false,
        data: {id:this.id},
        success: function(data) {
            $('.area1').html(data);
        },
        failure: function() {
            alert('Please try again..!');
        }
    });
});

now div content looks like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

       <div class="area1">
            <input type="text" placeholder='Name'>
            <button id='submit'>Submit</button>
        </div>

Now I want to perform some action on button click

$('.area1 button').click(function(e){
    e.preventDefault();
    alert(this.id) ;
});

but this second ajax is not selecting the button

Can anyone tell the correct method

>Solution :

$(document).on("click","div.area1 button",function(e) {
  e.preventDefault();
  alert(this.id) ;
});

you can try this

if your page was dynamically creating elements dosomething you would bind the event to a parent which already exists (this is the nub of the problem here, you need something that exists to bind to, don’t bind to the dynamic content), this can be (and the easiest option) is document.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading