jquery form submit with .on()

I'm trying to send a form created by jquery. The form is appended to a div and variable 'data' below is created using php, I'll just post the most important js code.

I tried many things with and without 'on()' but I fail in getting the alert box displaying the '1' so that I know the code block is actually executed..tbh I don't get what I'm doing wrong, any explanation would be greatly appreciated. Thanks in advance!

$(".r5").click(function(event){
    var data='<select name="sForum"><option value="1">bla</option></select>';
    $(this).parent().next('div').html('<form class="moveform">'+data+'<input type="submit" name="submit" class="movethisform" value="Move Thread" /></form>');
});
$("form.moveform").on("submit",function(event){
    alert(1);
});

Answers


You are binding to the form before it exists. Bind the event to the document, and pass the form selector to .on:

$(".r5").click(function(event){
    var data='<select name="sForum"><option value="1">bla</option></select>';
    $(this).parent().next('div').html('<form class="moveform">'+data+'<input type="submit" name="submit" class="movethisform" value="Move Thread" /></form>');
});
$(document).on("submit", "form.moveform", function(event){
    alert(1);
});

have you tried

$("inout.movethisform").on("click",function(event){
    alert(1);
    $("form.moveform").submit();
});

Need Your Help

Data bound radio button list in WPF

wpf radio-button databound

I have a list of options in a data object, and I want to make the equivalent of a radio button list to allow the user to select one and only one of them. Functionality similar to a databound combo...

Location permission alert on iPhone with PhoneGap

iphone cordova core-location

How do you change the string on the alert saying: