I have designed a form that incorporates dynamic functionality based on the selection made in a dropdown menu. When a user selects the 'roll' option from the dropdown, a set of input fields becomes visible, accompanied by a button. This button, labeled 'click to start inputting', serves as a trigger. Conversely, if the user chooses 'packingLeda' from the dropdown, the previously mentioned button disappears.
In the scenario where the 'roll' option is selected, I aim to introduce an additional set of input fields. This new set of fields should become visible only when the user interacts with the 'click to start inputting' button.
To ensure data integrity, I plan to implement validation checks on the input fields before allowing submission. Specifically, I want to restrict input to numerical values, allowing only floating-point numbers. This validation step will help maintain the accuracy of the data collected.
Ultimately, when the user submits the form, all the entered information across both sets of input fields should be collectively gathered and processed. This comprehensive submission process ensures that no data is overlooked and that the entire set of user-provided information is captured effectively.
The problem I'm having is when I click submit button everything disappears, the form handling does not work.
//// function below shows a button when an option is selected from dropdown list
$(function () {
$('#add').hide();
$('#itemname').change(function () {
var text = $(this).children("option:selected").text();
(text == "Roll" || text == "roll") ? $('#add').show() : $('#add').hide();
//// ( text == "Roll" || text == "packing Leda") this means you can loop through many options
});
});
//
////function below shows inputs to add roll weight
$("#add").click(function () {
newRowAdd =
'<div class="col-md-3 yyy"><div id="thisTest"><div class="form-group">' +
' <label for="example-text-input" id="message" class="form-control-label">Roll Weight</label>' +
'<small class="text-danger"></small>' +
'<button class="btn d-flex btn-danger btn-sm" type="button" id="DeleteRow">Delete</button>' +
'<input class="form-control" name="kgArray[]" id="kg" type="text" placeholder="type Kg">' +
'</div></div></div>';
$('#new').append(newRowAdd);
});
$("body").on("click", "#DeleteRow", function () {
$(this).parents("#thisTest").remove();
});
$('#itemname').change(function () {
var text = $(this).children("option:selected").text();
(text == "Roll" || text == "roll") ? $('#add').show() : $(".yyy").remove();
//// ( text == "Roll" || text == "packing Leda") this means you can loop through many options
});
//// function below shows all inputs button when an option is selected from dropdown list
$(function () {
$('.show').hide();
$('#itemname').change(function () {
var text = $(this).children("option:selected").text();
(text == "Roll" || text == "PackingLeda") ? $('.show').show() : $('.show').hide();
//// ( text == "Roll" || text == "packing Leda") this means you can loop through many options
});
});
function validateForm() {
var x = document.forms["addtoInventory"]["kgArray"].value;
if (x == "") {
alert("empty row must be deleted before submitting");
return false;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="card-body">
<form action="" method="post" name="addtoInventory" onsubmit="return validateForm()">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="item-name" class="form-control-label">Item name</label>
<small class="text-danger"></small>
<select class="form-select" name="itemname" id="itemname">
<option value=""></option>
<option value="roll">Roll</option>
<option value="PackingLeda">PackingLeda</option>
</select>
</div>
</div>
<div class="col-md-6 show">
<div class="form-group">
<label for="item-price" class="form-control-label">Item Price</label>
<small class="text-danger"></small>
<input class="form-control" name="itemprice" type="text" value="" placeholder="How much did it cost?">
</div>
</div>
<div class="col-md-6 show">
<div class="form-group">
<label for="item-quantity" class="form-control-label">Item Quantity</label>
<small class="text-danger"></small>
<input class="form-control" name="itemquantity" type="text" value="" placeholder="How many did you bought?">
</div>
</div>
<div class="col-md-12 show">
<div class="form-group">
<label for="purchased-from" class="form-control-label">Purchased From</label>
<small class="text-danger"></small>
<input class="form-control" name="purchasedfrom" type="text" value="" placeholder="Where did you buy it from?">
</div>
</div>
<div class="col-md-12 show">
<div class="form-group">
<label for="about-item" class="form-control-label">About Item</label>
<small class="text-danger"></small>
<input class="form-control" name="aboutitem" type="text" value="" placeholder="Write briefly about item">
</div>
</div>
<hr class="horizontal dark">
</div>
<div class="row" id="new"></div>
<button class="btn btn-success ms-0" id="add" type="button">Click to start inputting</button>
<button name="addI" class="btn d-flex btn-success btn-sm ms-auto">Submit</button>
</form>
</div>