1

I have a form loaded from AJAX. On this form there is a input type button which i would like to manage like it

$("#button_id").on("click",function(){..}); 

but it's does not work...

My question is how to do this work?

Javascript/Jquery load form code

function openForm('commandForm')
{
    $.ajax({
        type: "POST",
        url: "./forms/commandForm.php",
        data: $('#'+frm).
        dataType: "html",
        beforeSend: function(msg){
            $('#ResponseDiv').html('Loding....');
        },
        success: function(back_data){
            $('#ResponseDiv').html(back_data);
        }
    });
}

commandForm.php

<form id="commandForm" action="postCommand.php" method="post" >
    .....
    ....
    <button type="Button" id="postButton" />

</form>

Jquery on postButton onclick

$(document).ready(function () {
    $('#postButton').on("click", function (e) {
        /*....*/
    });  //This don't work
}

PS: I don't want submit the form directly! Just make my own checkform running in my javascript function...out of here. So i want only that the button react on the Onclick ...so to do an alert for example or any javacript function else

Ray Carnegie
  • 103
  • 1
  • 7
  • 2
    You probably have a typo in your html: `"postButton " !== "postButton"`. Notice the space at the end. – jeroen Feb 17 '19 at 09:30
  • 1
    I see that you have an empty space at the end of your postButton id, remove the space too. – arielb Feb 17 '19 at 09:31
  • 1
    @MagnusEriksson yes i have a 'e.preventDefault()' my attemp here it 's not to sumbit the form but to improve an action like checkform() for exemple. Not directly submit the form. i'd like only the the javascript onclick button react. for alert() e.g or any javascript reaction – Ray Carnegie Feb 17 '19 at 09:40
  • @RaymondGbely - Forget my first (now deleted) comment. I didn't properly read the code. But check the other comments. They have spotted an issue that might be the cause. – M. Eriksson Feb 17 '19 at 09:41
  • @MagnusEriksson ok. – Ray Carnegie Feb 17 '19 at 09:43

2 Answers2

2

Solved my problem with jquery event delagation

More infos here.

Event binding on dynamically created elements?

Ray Carnegie
  • 103
  • 1
  • 7
0

Now i understood. This will help you-

$(document).ready(function () {
$("#id").click(function (e) {
//.........
});  //This will work
}