$('#inputqty').on('change keyup',function(){
let inputs='';
let value = parseInt($(this).val());
if (0<value) {
for (let i = 0; i<value; i++) {
inputs+=`<div style="margin-left: 10px; margin-top: 10px;">
<input autocomlete="off" type="text" style="margin-bottom:5px;margin-top:5px; margin-left:10px;" name="item_serial" id="serial_number" placeholder=" Serial Number. item:${i+1}" tabindex="${i+6+1}" required /></br>
</div>`
}
}
else{
inputs+=`<center>No item Quantity inputted!!!<center>`
}
$('#contentforserial').html(inputs);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" min="1" max="10"id="inputqty" name="inputqty">
<div style="height: 190px;width:260px;padding:3px; background:greenyellow;" id="containerforserial">
<div>Enter Serial Number of Item:</div>
<div class="contentforserial" id="contentforserial" style=" height:150px;overflow:auto;background:#fff;">
<div id="demo"></div>
</div>
</div>
3
Answers
Create an
object
which will be used as database to store input values. Loop through input elements and store value as key in theobject
and an array as key. Push input element in the array.Then loop through
object
values (array of input elements) and if array contains more than one input element, that means these input elements have duplicate values, do whatever you want with them:And please avoid using bloatware jquery and inline styles.
You can create one function to check duplicate value then on your input add onchange event call to this function
Example
Then add class and on change event to your input
So, your final code should be
Add a class name to your input text inside the loop then
this array will give you values of input fields, you can validate it, and add the validation error message to the relevant input field using the index of the array.