How can I submit this form after replacing the original submit button with a jQuery generated submit button?
Form:
<form action="url" method="post" accept-charset="utf-8" class="form horizontal" id="reg">
<select name ="type" id="owner">
<option...>
</select>
<div class="controls-actions">
<button type="submit" id="sub">Submit</button>
</div>
</form>
There is an event listener to add fields to the form:
$('#owner').change(function(){
if($('#owner').val() == 1){
$('#owner').after('<input name="added" value="2">');
}
})
Now, if I use the default submit button, the new field doesn't get posted, so I tried using .submit():
//remove the old submit button and add new one
$('.control-actions').empty().append('<button id="added">Submit>');
Finally add an event listener to the newly generated button to submit the form:
$('#added').on('click', function(){
$('#reg').submit();
})
Nothing happens when the newly generated button is clicked, no console errors, no form submission. I have also tried click(), delegate() etc. jQuery version is 1.10
$('.control-actions')isn't matching anything, because the class is "controls-actions.".empty().append(...)when you could simply.html(...).