I'm trying to dynamically add a row, with a form, inside a table, upon the click of a button. Here's the JS code chunk:
$(document).ready(function () {
window.can_add_bank_row = true;
$('#add_bank_acc_btn').on('click', function () {
addBankAccRow();
});
});
/**
* Adds a table row with fields to add a bank account to the user's profile.
*/
function addBankAccRow() {
if (window.can_add_bank_row) {
var warning_msg = "You are about to add a new bank account for this user.\n\nAre you sure?";
$('<tr>' +
'<form id="addNewBankAccountForm" method="POST" onsubmit=\"return confirm(\'' + warning_msg + '\');\">' +
'</tr>').insertBefore('#bank_accs_tbl > tbody > tr:first');
$('#addNewBankAccountForm').html(
'<td></td>' +
'<td><select id="bank_type_dropdown">' +
'<option value="international">International</option>' +
'<option value="sepa">SEPA</option>' +
'<option value="interac">Interac</option>' +
'<option value="paypal">PayPal</option>' +
'</select></td>' +
'<td><input name="add_bank_label_input" type="text"></td>' +
'<td><input name="add_bank_currency_input" type="text" style="width: 50px;"></td>' +
'<td><input name="add_bank_min_transfer_input" type="text" style="width: 80px;"></td>' +
'<td><input type="checkbox" name="add_bank_approved" value="0"></td>' +
'<td><button type="submit" class="btn btn-primary">Add</button></td>'
);
window.can_add_bank_row = false;
} else {
alert('You\'ve got one empty row to add a bank account already.\nPlease, fill and submit it before adding a new one. :)');
}
}
When I check the source of the page, I can see the form and all its elements in the code, but it just doesn't appear visually. // jsfiddle for this ^
If I try this instead:
$('<tr>' +
'<form method="POST" onsubmit=\"return confirm(\'' + warning_msg + '\');\">' +
'<td></td>' +
'<td><select id="bank_type_dropdown">' +
'<option value="international">International</option>' +
'<option value="sepa">SEPA</option>' +
'<option value="interac">Interac</option>' +
'<option value="paypal">PayPal</option>' +
'</select></td>' +
'<td><input name="add_bank_label_input" type="text"></td>' +
'<td><input name="add_bank_currency_input" type="text" style="width: 50px;"></td>' +
'<td><input name="add_bank_min_transfer_input" type="text" style="width: 80px;"></td>' +
'<td><input type="checkbox" name="add_bank_default" value="0"></td>' +
'<td><input type="checkbox" name="add_bank_approved" value="0"></td>' +
'<td><button type="submit" class="btn btn-primary">Add</button></td>' +
'</form>' +
'</tr>').insertBefore('#bank_accs_tbl > tbody > tr:first');
then the form tag is auto-closed and the input fields and the submit button are left outside the form...
Aaaand, I also tried this one:
$('<form id="addNewBankAccountForm" method="POST" onsubmit=\"return confirm(\'' + warning_msg + '\');\">'
).insertBefore('#bank_accs_tbl > tbody > tr:first');
$('#addNewBankAccountForm').html(
'<tr>' +
'<td></td>' +
'<td><select id="bank_type_dropdown">' +
'<option value="international">International</option>' +
'<option value="sepa">SEPA</option>' +
'<option value="interac">Interac</option>' +
'<option value="paypal">PayPal</option>' +
'</select></td>' +
'<td><input name="add_bank_label_input" type="text"></td>' +
'<td><input name="add_bank_currency_input" type="text" style="width: 50px;"></td>' +
'<td><input name="add_bank_min_transfer_input" type="text" style="width: 80px;"></td>' +
'<td><input type="checkbox" name="add_bank_approved" value="0"></td>' +
'<td><button type="submit" class="btn btn-primary">Add</button></td>' +
'</tr>'
);
Now the row, along with the inputs, are visualised, but all in the first cell... I can't handle front-end, I'm a backend guys. Please help :D