I am having a problem removing an element from a form when a select input option is changed. When the select input is placed to "Manually Specify" the javascript correctly adds to the form as i would expect. When the option is changed again to anything other than "Manually Specify i would like it to remove the form inputs previously added, but can not seem to get this to work.
$(function() { /* DOM ready */
$("#distanceSelect").change(function() {
if ($(this).val() == "Manually Specify") {
var html = $("#distanceSpecifyInput").html();
$("#distanceSelectInput").after(html);
} else {
var child = document.getElementById("distanceSpecifyInput");
if (child) {
child.parentNode.removeChild(child);
}
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<form>
<div class="form-group row" id="distanceSelectInput">
<div class="col-sm-3">
<label for="distanceSelect" class="control-label">Distance:</label>
<select class="form-control" id="distanceSelect">
<option>5km</option>
<option>10km</option>
<option>Half Marathon</option>
<option>Marathon</option>
<option>Manually Specify</option>
</select>
</div>
</div>
<div class="hide" id="distanceSpecifyInput">
<div class="form-group row">
<div class="col-sm-1">
<input type="number" class="form-control" step="0.1" id="distance">
</div>
<div class="col-sm-2">
<select class="form-control" name="distanceFormat" id="distanceFormat">
<option>miles</option>
<option>km</option>
<option>meters</option>
</select>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-12">
<label for="hours" class="control-label">Desired Finish Time:</label>
</div>
<div class="col-sm-1">
<input type="number" class="form-control" required step="1" placeholder="hours" required name="hours" id="hours">
</div>
<div class="col-sm-1">
<input type="number" class="form-control" required step="1" placeholder="mins" required name="mins" id="mins">
</div>
<div class="col-sm-1">
<input type="number" class="form-control" required step="1" placeholder="secs" required name="secs" id="secs">
</div>
</div>
<div class="form-group">
<button type="button" class="btn btn-default" onclick="calculateRacePace()">Submit</button>
</div>
</form>
</div>
Thanks for any help.
You should create a specific DIV to hold the content, rather than using .after(). That way you can clear it with .empty().
$(function() { /* DOM ready */
$("#distanceSelect").change(function() {
if ($(this).val() == "Manually Specify") {
var html = $("#distanceSpecifyInput").html();
$("#distanceOutput").html(html);
} else {
$("#distanceOutput").empty();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<form>
<div class="form-group row" id="distanceSelectInput">
<div class="col-sm-3">
<label for="distanceSelect" class="control-label">Distance:</label>
<select class="form-control" id="distanceSelect">
<option>5km</option>
<option>10km</option>
<option>Half Marathon</option>
<option>Marathon</option>
<option>Manually Specify</option>
</select>
</div>
</div>
<div class="hide" id="distanceSpecifyInput">
<div class="form-group row">
<div class="col-sm-1">
<input type="number" class="form-control" step="0.1" id="distance">
</div>
<div class="col-sm-2">
<select class="form-control" name="distanceFormat" id="distanceFormat">
<option>miles</option>
<option>km</option>
<option>meters</option>
</select>
</div>
</div>
</div>
<div id="distanceOutput">
</div>
<div class="form-group row">
<div class="col-sm-12">
<label for="hours" class="control-label">Desired Finish Time:</label>
</div>
<div class="col-sm-1">
<input type="number" class="form-control" required step="1" placeholder="hours" required name="hours" id="hours">
</div>
<div class="col-sm-1">
<input type="number" class="form-control" required step="1" placeholder="mins" required name="mins" id="mins">
</div>
<div class="col-sm-1">
<input type="number" class="form-control" required step="1" placeholder="secs" required name="secs" id="secs">
</div>
</div>
<div class="form-group">
<button type="button" class="btn btn-default" onclick="calculateRacePace()">Submit</button>
</div>
</form>
</div>
Note that copying the HTML like this results in duplicate IDs distance and distanceFormat. IDs are supposed to be unique, so you should fix up the IDs after copying.
Related
I have three input fields I want to get the sum of two input fields and put the result 3rd input field like this.
<div class="row mt-2">
<div class="col-md-2">
<label for="subTotal" class="form-label">SubTotal</label>
</div>
<div class="col-md-2">
<input type="number" class="form-control" id="subTotal" required>
</div>
</div>
<div class="row mt-2">
<div class="col-md-2">
<label for="discount" class="form-label">Discount</label>
</div>
<div class="col-md-2">
<input type="number" class="form-control" id="discount" required>
</div>
<div class="col-md-2">
<input type="number" class="form-control" id="discountResult">
</div>
</div>
In this code, I want to add the Subtotal value with discount value and show the result in the discount input text field.
I created a function below using your HTML.
It will take the value of subtotal and subtract the discount value onchange.
var subTotal = document.getElementById('subTotal');
var discount = document.getElementById('discount');
var discountResult = document.getElementById('discountResult');
discount.addEventListener("change", function() {
discountResult.value = subTotal.value - discount.value;
});
<div class="row mt-2">
<div class="col-md-2">
<label for="subTotal" class="form-label">SubTotal</label>
</div>
<div class="col-md-2">
<input type="number" class="form-control" id="subTotal" required>
</div>
</div>
<div class="row mt-2">
<div class="col-md-2">
<label for="discount" class="form-label">Discount</label>
</div>
<div class="col-md-2">
<input type="number" class="form-control" id="discount" required>
</div>
<div class="col-md-2">
<input type="number" class="form-control" id="discountResult">
</div>
</div>
<div class="row mt-2">
<div class="col-md-2">
<label for="subTotal" class="form-label">SubTotal</label>
</div>
<div class="col-md-2">
<input type="number" class="form-control" id="subTotal" required>
</div>
</div>
<div class="row mt-2">
<div class="col-md-2">
<label for="discount" class="form-label">Discount</label>
</div>
<div class="col-md-2">
<input type="number" class="form-control" id="discount" required>
</div>
<div class="col-md-2">
<input type="number" class="form-control" id="discountResult">
</div>
</div>
function sum()
{
var subTotal = document.getElementById("subTotal");
var discount = document.getElementById("discount");
var result=subTotal+discount;
document.getElementById("discountResult").value = result;
}
I have two different forms with id newForm and oldForm . But when i click on submit button then html5 validation is only shown on input fields of 'newForm'.The html5 validation is not appearing in oldForm ,as i have kept the required field in input fileds of oldForm. How can i validate these two different forms when i click Submit button ? I need the html5 valdiation in both forms but the validation is appearing in seconnd form but not in first form.
$(document).ready(function() {
$("#saveBtn").on("click", function() {
console.log("form submitted");
/*$.ajax({
// ajax code to submit
}); */
});
});
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="padding-bottom: 20px;">
<h2>Pre Assessment</h2>
<div class="card">
<div class="card-body">
<div class="col-md-12" style="float: none;">
<form id="oldForm">
<div class="row">
<div class="form-group col-md-6 assess">
<div class="col-md-12">
<font face="preeti" size="5">s/ lgwf{/0f ug{ kg]{ sf/0f
</font>
<select class="form-control" name="causeOfExciseAct" id="causeOfExciseAct">
<option value="" selected disabled hidden>Choose here</option>
<option value="appeal">Appeal</option>
<option value="other">Other</option>
</select>
</div>
</div>
<div class="form-group col-md-6">
<div class="row">
<div class="col-md-12 pnbp" style="margin-bottom: 10px;">
<font face="preeti" size="5">
k'g/fj]bgsf] lg0f{o cg';f/ ePsf] eP k'g/fj]bg g+ </font>
<input type="text" class="form-control" id="appealId" name="appealId" required />
</div>
</div>
<div class="row">
<div class="col-md-12 orIf" style="margin-bottom: 10px;">
<font face="preeti" size="5">cGo</font>
<input type="text" class="form-control" id="reasonDesc" name="reasonDesc" required />
</div>
</div>
</div>
</div>
<!-- for other two field -->
<div class="row">
<div class="form-group col-md-3">
<div class="col-md-12" style="margin-bottom: 10px;">
(B.S.)
<input type="text" class="form-control" onchange="changeToAd(this.value,document.getElementById('intCalUptoAd').id)" id="intCalUpto" name="intCalUpto" required>
</div>
</div>
<div class="form-group col-md-3">
<div class="col-md-12" style="margin-bottom: 10px;">
<font face="preeti" size="5">
</font>(A.D.)
<input type="text" class="form-control" onchange="changeToBs(this.value,document.getElementById('intCalUpto').id)" id="intCalUptoAd" name="intCalUptoAd" required>
</div>
</div>
<div class="form-group col-md-3">
<div style="margin-bottom: 40px;">
(B.S)
<input type="text" class="form-control" onchange="changeToAd(this.value,document.getElementById('assessmentDateAd').id)" id="assessmentDate" name="assessmentDate">
</div>
</div>
<div class="form-group col-md-3">
<div style="margin-bottom: 40px;">
(A.D)
<input type="text" class="form-control" onchange="changeToBs(this.value,document.getElementById('assessmentDate').id)" id="assessmentDateAd" name="assessmentDateAd">
</div>
</div>
</div>
</form>
<form id="newForm">
<div id="formContainer" class="col-md-12" style="float: none;">
<div style="margin-bottom: 30px;">
<div class="form-group row">
<div class="col-md-1"></div>
<div class="col-md-4">
<label>Reason</label>
</div>
<div class="col-md-2">
<label>Amount</label>
</div>
<div class="col-md-2">
<label>Penalty</label>
</div>
<div class="col-md-1">Total</div>
<div class="col-md-2">Action</div>
</div>
<div class="customs-table row">
<div class="col-md-1">
<label>Customs</label>
</div>
<div class="col-md-4">
<input type="text" class="form-control customReason" required />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt customAmount" value="0" name="abc" min="0" required />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt customPenalty" value="0" name="abc" min="0" required />
</div>
<div class="col-md-1">
<span class="sum">0</span>
</div>
<div class="col-md-2">
<button type="button" class="add">+</button>
<button type="button" class="remove">-</button>
</div>
</div>
<div class="vat-table row">
<div class="col-md-1">
<label>VAT</label>
</div>
<div class="col-md-4">
<input type="text" class="form-control vatReason" name="vatReason" />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt1 vatAmount" value="0" name="vatAmount" min="0" />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt1 vatPenalty" value="0" name="vatPenalty" min="0" />
</div>
<div class="col-md-1">
<span class="sum">0</span>
</div>
<div class="col-md-2">
<button type="button" class="add">+</button>
<button type="button" class="remove">-</button>
</div>
</div>
<div class="excise-table row">
<div class="col-md-1">
<label>Excise</label>
</div>
<div class="col-md-4">
<input type="text" class="form-control exciseReason" name="exciseReason" />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt2 exciseAmount" value="0" name="exciseAmount" min="0" />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt2 excisePenalty" value="0" name="excisePenalty" min="0" />
</div>
<div class="col-md-1">
<span class="sum">0</span>
</div>
<div class="col-md-2">
<button type="button" class="add">+</button>
<button type="button" class="remove">-</button>
</div>
</div>
<div id="a"></div>
<div class="col-md-12 pull-right">
<label>Total:</label> <b><span id="tot">0</span></b>
</div>
</div>
<button id="saveBtn" class="btn btn-success pull-right">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
Since you're using HTML5 validation, use HTML5 DOM methods:
$(document).ready(function() {
// Add the e (event) argument to your event handler...
$("#saveBtn").on("click", function(e) {
// and call preventDefault() on it to prevent submission of the form.
e.preventDefault();
let allValid = true;
$("form").each(function (index, form) {
allValid = allValid && form.reportValidity();
});
if (allValid) {
/*$.ajax({
// ajax code to submit
}); */
}
});
});
form { border: 1px solid blue; }
input:invalid { border: 1px solid red; }
<!DOCTYPE html>
<html>
<head>
<title></title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="padding-bottom: 20px;">
<h2>Pre Assessment</h2>
<div class="card">
<div class="card-body">
<div class="col-md-12" style="float: none;">
<form id="oldForm">
<div class="row">
<div class="form-group col-md-6 assess">
<div class="col-md-12">
<font face="preeti" size="5">s/ lgwf{/0f ug{ kg]{ sf/0f
</font>
<select class="form-control" name="causeOfExciseAct" id="causeOfExciseAct">
<option value="" selected disabled hidden>Choose here</option>
<option value="appeal">Appeal</option>
<option value="other">Other</option>
</select>
</div>
</div>
<div class="form-group col-md-6">
<div class="row">
<div class="col-md-12 pnbp" style="margin-bottom: 10px;">
<font face="preeti" size="5">
k'g/fj]bgsf] lg0f{o cg';f/ ePsf] eP k'g/fj]bg g+ </font>
<input type="text" class="form-control" id="appealId" name="appealId" required />
</div>
</div>
<div class="row">
<div class="col-md-12 orIf" style="margin-bottom: 10px;">
<font face="preeti" size="5">cGo</font>
<input type="text" class="form-control" id="reasonDesc" name="reasonDesc" required />
</div>
</div>
</div>
</div>
<!-- for other two field -->
<div class="row">
<div class="form-group col-md-3">
<div class="col-md-12" style="margin-bottom: 10px;">
(B.S.)
<input type="text" class="form-control" onchange="changeToAd(this.value,document.getElementById('intCalUptoAd').id)" id="intCalUpto" name="intCalUpto" required>
</div>
</div>
<div class="form-group col-md-3">
<div class="col-md-12" style="margin-bottom: 10px;">
<font face="preeti" size="5">
</font>(A.D.)
<input type="text" class="form-control" onchange="changeToBs(this.value,document.getElementById('intCalUpto').id)" id="intCalUptoAd" name="intCalUptoAd" required>
</div>
</div>
<div class="form-group col-md-3">
<div style="margin-bottom: 40px;">
(B.S)
<input type="text" class="form-control" onchange="changeToAd(this.value,document.getElementById('assessmentDateAd').id)" id="assessmentDate" name="assessmentDate">
</div>
</div>
<div class="form-group col-md-3">
<div style="margin-bottom: 40px;">
(A.D)
<input type="text" class="form-control" onchange="changeToBs(this.value,document.getElementById('assessmentDate').id)" id="assessmentDateAd" name="assessmentDateAd">
</div>
</div>
</div>
</form>
<form id="newForm">
<div id="formContainer" class="col-md-12" style="float: none;">
<div style="margin-bottom: 30px;">
<div class="form-group row">
<div class="col-md-1"></div>
<div class="col-md-4">
<label>Reason</label>
</div>
<div class="col-md-2">
<label>Amount</label>
</div>
<div class="col-md-2">
<label>Penalty</label>
</div>
<div class="col-md-1">Total</div>
<div class="col-md-2">Action</div>
</div>
<div class="customs-table row">
<div class="col-md-1">
<label>Customs</label>
</div>
<div class="col-md-4">
<input type="text" class="form-control customReason" required />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt customAmount" value="0" name="abc" min="0" required />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt customPenalty" value="0" name="abc" min="0" required />
</div>
<div class="col-md-1">
<span class="sum">0</span>
</div>
<div class="col-md-2">
<button type="button" class="add">+</button>
<button type="button" class="remove">-</button>
</div>
</div>
<div class="vat-table row">
<div class="col-md-1">
<label>VAT</label>
</div>
<div class="col-md-4">
<input type="text" class="form-control vatReason" name="vatReason" />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt1 vatAmount" value="0" name="vatAmount" min="0" />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt1 vatPenalty" value="0" name="vatPenalty" min="0" />
</div>
<div class="col-md-1">
<span class="sum">0</span>
</div>
<div class="col-md-2">
<button type="button" class="add">+</button>
<button type="button" class="remove">-</button>
</div>
</div>
<div class="excise-table row">
<div class="col-md-1">
<label>Excise</label>
</div>
<div class="col-md-4">
<input type="text" class="form-control exciseReason" name="exciseReason" />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt2 exciseAmount" value="0" name="exciseAmount" min="0" />
</div>
<div class="col-md-2">
<input type="number" class="form-control txt2 excisePenalty" value="0" name="excisePenalty" min="0" />
</div>
<div class="col-md-1">
<span class="sum">0</span>
</div>
<div class="col-md-2">
<button type="button" class="add">+</button>
<button type="button" class="remove">-</button>
</div>
</div>
<div id="a"></div>
<div class="col-md-12 pull-right">
<label>Total:</label> <b><span id="tot">0</span></b>
</div>
</div>
<button id="saveBtn" class="btn btn-success pull-right">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
Things to note:
This will show the validation message on the last invalid field on the last form in document order.
I added CSS to highlight the form boundaries and invalid fields to make it more obvious.
The font element has been deprecated for a long time. Use a span and CSS.
Don't use HTML5 validation, instead create your own validation methods using Ajax to prevent client side user manipulation.
Using JavaScript to validate the forms before POST as follows:
<!--Html Form-->
<form name="contact-form" action="" method="post" onsubmit="return checkFields()">
<label for="nameinput">Name</label>
<input type="text" name="name">
<label for="emailinput">Email address</label>
<input type="email" name="email">
<label for="textarea-input">Message</label>
<textarea rows="5" name="message"></textarea>
<button type="submit" name="contactSubmitBtn">Submit</button>
</form>
function checkFields() {
var x = document.forms["example-form"]["name"].value;
var y = document.forms["example-form"]["email"].value;
var z = document.forms["example-form"]["message"].value;
if (x == "") {
Response Here
return false;
}
if (y == "") {
Response Here
return false;
}
if (z == "") {
Response Here!
return false;
}
}
Actually am making a online quiz func in my website here is my quiz code
<form method="POST">
<div id="questions">
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<label>Question <label id="question_no" value='1'>1</label></label>
<input type="text" name="question" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<input type="text" name="option1" class="form-control" placeholder="Option A">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<input type="text" name="option1" class="form-control" placeholder="Option B">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<input type="text" name="option1" class="form-control" placeholder="Option C">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<input type="text" name="option1" class="form-control" placeholder="Option D">
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-success mb-4">Submit</button> <button id="add_more" class="btn btn-primary mb-4">Add More</button>
</form>
actually i want that when add more button is clicked then the same code in div with id questions is append next to this div and also question no will be increased automatically
i tried diff method but none of them works prefectly and help
Thank You ...
~~~~~~~UPDATE~~~~~~~~
here is what i do simply wrote this code
<!--TEMPLATE-->
<script type="text/javascript" id="questions_template">
<div id="questions_template1">
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<label>Question 1</label>
<input type="text" name="question" class="form-control">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<input type="text" name="option1" class="form-control" placeholder="Option A">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<input type="text" name="option1" class="form-control" placeholder="Option B">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<input type="text" name="option1" class="form-control" placeholder="Option C">
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<input type="text" name="option1" class="form-control" placeholder="Option D">
</div>
</div>
</div>
</div>
</script>
<!--END OF TEMPLATE-->
and after that here is my jquery code
$("#add_more").click(function(e){
e.preventDefault();
$("#questions").append($("#questions_template").html());
})
First, add div for your template question, this is the one you will duplicate.
<form method="POST">
<div class="question">
.....
</div>
<button>submit</button>
<button>add_more</button>
</form>
Now use jquery to duplicate this 'question' and update it with correct id.
$("#add_more").click(function(e) {
e.preventDefault();
var id = $(".question").length;
id++;
var $newQuestion = $("form").find(".question:last").clone();
$newQuestion.find("label").attr("value", id);
$newQuestion.find("#question_no").text(id);
$newQuestion.insertBefore($(".btn-success"));
})
Jsfiddle : https://jsfiddle.net/xpvt214o/411294/
I have this fiddle https://jsfiddle.net/74mh06v9/ where I am stuck on how to repeat the form group?
This code I am using is as below;
$(function () {
$(".repeat").on('click', function (e) {
e.preventDefault();
var $self = $(this);
$self.before($self.prev('table').clone());
});
});
My HTML is
<div class="col-md-12" class="repeatable">
<h4>Invoice Details </h4>
<table>
//various form groups here to repeat
</table>
<button class="repeat">Add Another</button>
First of all your html ws wrong inside table there should be td,tr etc but u placed div's i dont kno why. i hv corrected it
its working now.
HTML:
<div class="col-md-12" class="repeatable">
<div class="base-group" style="display:none;">
<h4>Invoice Details </h4>
<div class="col-sm-3">
<div class="form-group">
<label>Service User</label>
<div class="input-group">
<input name="service_user" id="service_user" class="form-control" type="text"></div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Hours</label>
<div class="input-group">
<input name="total_hours" id="total_hours" class="form-control" type="text">
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Rate</label>
<div class="input-group">
<input name="rate" id="rate" class="form-control" type="text">
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Sub Total</label>
<div class="input-group">
<input name="invoice_amount" id="invoice_amount" class="form-control" type="text">
</div>
</div>
</div>
</div>
</div>
<div id="main-form">
<div class="child-group">
<h4>Invoice Details </h4>
<div class="col-sm-3">
<div class="form-group">
<label>Service User</label>
<div class="input-group">
<input name="service_user" id="service_user" class="form-control" type="text"></div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Hours</label>
<div class="input-group">
<input name="total_hours" id="total_hours" class="form-control" type="text">
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Rate</label>
<div class="input-group">
<input name="rate" id="rate" class="form-control" type="text">
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Sub Total</label>
<div class="input-group">
<input name="invoice_amount" id="invoice_amount" class="form-control" type="text">
</div>
</div>
</div>
</div>
</div>
</div>
<button type="button" class="repeat">Add Another</button>
JS:
$(function() {
$(".repeat").on('click', function(e) {
var c = $('.base-group').clone();
c.removeClass('base-group').css('display','block').addClass("child-group");
$("#main-form").append(c);
});
});
fiddle https://jsfiddle.net/riazxrazor/74mh06v9/9/
if you want to try
Firslty, there is no need of table(no rows and columns), and replace table to div and add a class frm to it, then make a clone and append it.
$(function() {
$(".repeat").on('click', function(e) {
var frm =$('.frm:first').clone();
frm.find('input').val('');
$('.frm:last').after(frm);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-12 repeatable">
<div class="frm">
<h4>Invoice Details </h4>
<div class="col-sm-3">
<div class="form-group">
<label>Service User</label>
<div class="input-group">
<input name="service_user" id="service_user" class="form-control" type="text"></div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Hours</label>
<div class="input-group">
<input name="total_hours" id="total_hours" class="form-control" type="text">
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Rate</label>
<div class="input-group">
<input name="rate" id="rate" class="form-control" type="text">
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Sub Total</label>
<div class="input-group">
<input name="invoice_amount" id="invoice_amount" class="form-control" type="text">
</div>
</div>
</div>
</div>
</div>
<button class="repeat">Add Another</button>
Slight changes to the answer above:
<script>
$(function () {
$(".repeat").on('click', function (e) {
e.preventDefault();
var clonedForm = $(".frm").clone();
clonedForm.appendTo("#table");
frm.find('input').val('');
});
});
</script>
<div class="col-md-12" class="repeatable">
<table id ="table">
<div class="frm">
<h4>Invoice Details </h4>
<div class="col-sm-3">
<div class="form-group">
<label>Service User</label>
<div class="input-group">
<input name="service_user" id="service_user" class="form-control" type="text"></div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Hours</label>
<div class="input-group">
<input name="total_hours" id="total_hours" class="form-control" type="text">
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Rate</label>
<div class="input-group">
<input name="rate" id="rate" class="form-control" type="text">
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<label>Sub Total</label>
<div class="input-group">
<input name="invoice_amount" id="invoice_amount" class="form-control" type="text">
</div>
</div>
</div>
</div>
</div>
</table>
<button class="repeat">Add Another</button>
what i want is that when i click on button "find reservation", the form submit should not refresh the page. Instead it should enable some fields in find reservation form underneath. But I am not able to achieve that. I am using bootstrap.
Here is my html part:-
<div class="container">
<div class="jumbotron checkInGuest">
<h3 class="h3Heading">Request for following information from guest</h3>
<form class="form-horizontal" id="checkInForm">
<div class="form-group">
<label for="reservationId" class="col-md-4">Reservation Id</label>
<div class="col-md-8">
<input type="text" class="form-control" id="reservationId" name="reservationId" required>
</div>
</div>
<div class="form-group">
<label for="dlNum" class="col-md-4">Driver License #</label>
<div class="col-md-8">
<input type="number" class="form-control" id="dlNum" min="0" name="dlNum" required>
</div>
</div>
<div class="form-group">
<div class="col-md-2">
<button type="submit" class="btn btn-success" id="findResButton">Find Reservation</button>
</div>
</div>
</form>
<!-- Form when info is found. Initially all fields are disabled-->
<div class="clear clearWithBorder"></div>
<h3 class="h3Heading">Information Found</h3>
<form class="form-horizontal">
<div class="form-group">
<label for="resId" class="col-md-3">Reservation Id</label>
<div class="col-md-3">
<input type="text" class="form-control" id="resId" name="resId" disabled>
</div>
<label for="dlNumReadOnly" class="col-md-3">Driver License #</label>
<div class="col-md-3">
<input type="number" class="form-control" id="dlNumReadOnly" min="0" name="dlNumReadOnly" disabled>
</div>
</div>
<div class="form-group">
<label for="guestFullName" class="col-md-3">Guest Full Name</label>
<div class="col-md-3">
<input type="text" class="form-control" id="guestFullName" name="guestFullName" disabled>
</div>
<label for="email" class="col-md-3">Email</label>
<div class="col-md-3">
<input type="email" class="form-control" id="email" name="email" disabled>
</div>
</div>
<div class="form-group">
<label for="numRooms" class="col-md-3">Rooms Booked</label>
<div class="col-md-3">
<input type="number" class="form-control readable" id="numRooms" name="numRooms" disabled>
</div>
<label for="roomType" class="col-md-1">Room Type</label>
<div class=" col-md-2">
<label for="smoking">
<input type="radio" name="roomType" id="smoking" class="roomType readable" disabled> Smoking
</label>
</div>
<div class=" col-md-3">
<label for="nonSmoking">
<input type="radio" name="roomType" id="nonSmoking" class="roomType readable" disabled>Non-Smoking
</label>
</div>
</div>
<div class="form-group">
<label for="discount" class="col-md-3">Discount</label>
<div class="col-md-3">
<select class="form-control readable" id="discount" disabled>
<option selected>0%</option>
<option>10%</option>
<option>20%</option>
<option>30%</option>
</select>
</div>
<label for="checkInDate" class="col-md-3">CheckIn Date</label>
<div class="col-md-3">
<input type="date" class="form-control readable" id="checkInDate" name="checkInDate" disabled>
</div>
</div>
<div class="form-group">
<label for="checkOutDate" class="col-md-3">CheckOut Date</label>
<div class="col-md-9">
<input type="date" class="form-control readable" id="checkOutDate" name="checkInDate" disabled>
</div>
</div>
<div class="form-group">
<div class="col-md-2">
<button type="button" class="btn btn-success" id="roomOrdButton">Confirm Room Order</button>
</div>
</div>
</form>
<div class="clear clearWithBorder"></div>
<h3 class="h3Heading">Final Room Order</h3>
<form class="form-horizontal">
<div class="form-group">
<label for="perInEachRoom" class="col-md-12">Number of people in each room</label>
<div class="col-md-8 " id="perInEachRoom">
</div>
<div class="form-group">
<div class="col-md-2">
<button type="button" class="btn btn-success" id="checkInButton">Check In</button>
</div>
</div>
</div>
</form>
</div>
</div>
And this is jquery part:-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
$("#checkInForm").validator();
$("#findResButton").click(function(e){
e.preventDefault();
$(".readable").prop("disabled",false);
});
$("#roomOrdButton").click(function(){
var numberOfRooms=$("#numRooms").val();
var counter=0;
var container=$('<div/>');
$(".readable").prop("disabled",true);
for(counter=1;counter<=numberOfRooms;counter++){
container.append('<label for="room">Room '+counter+'</label><input type="number" class="form-control readable" name="checkInDate" />');
}
$("#perInEachRoom").html(container);
});
$("#checkInButton").click(function(){
$("#perInEachRoom").html("");
});
</script>
You need to put your code in a document.ready() function, so:
$(document).ready(function() {
$("#checkInForm").validator();
$("#findResButton").click(function(e){
e.preventDefault();
$(".readable").prop("disabled",false);
});
$("#roomOrdButton").click(function(){
var numberOfRooms=$("#numRooms").val();
var counter=0;
var container=$('<div/>');
$(".readable").prop("disabled",true);
for(counter=1;counter<=numberOfRooms;counter++){
container.append('<label for="room">Room '+counter+'</label><input type="number" class="form-control readable" name="checkInDate" />');
}
$("#perInEachRoom").html(container);
});
$("#checkInButton").click(function(){
$("#perInEachRoom").html("");
});
});
You should also have a look at this question: Form is submitted when I click on the button in form. How to avoid this?
Long story short, you should use
<button type="button"...
not
<button type="submit"...
Actually, all I did is:-
$("#checkInForm").submit(function(e)
{
e.preventDefault();
});
And that solved it. I didnt have to change button type or anything. Basically, in future when I will make AJAX calls I will have the code underneath preventDefault statement.