I have a row with 4 inputs:
<div id="productRow1" class="productRow">
<input type="text" name="numbers[]">
<input type="number" name="quantities[]">
<input type="text" name="levels[]">
<input type="number" name="prices[]">
</div>
I have an "Add Another Product" button and user can add another row by clicking that button. Generated row will be like this with the unique div id:
<div id="productRow2" class="productRow">
<input type="text" name="numbers[]">
<input type="number" name="quantities[]">
<input type="text" name="levels[]">
<input type="number" name="prices[]">
</div>
I have a wrapper div for all the rows like this:
<div class="input_fields_wrap">
<div id="productRow1" class="productRow">...</div>
<div id="productRow2" class="productRow">...</div>
...
</div>
How I add new rows dynamically with the unique div row id when the "Add Another Product" button is clicked:
var wrapper = $(".input_fields_wrap");
$(wrapper).append('<div ' + divId + ' class="productRow"' + '>
<input type="text" name="numbers[]"/>' +
'<input type="number" name="quantities[]"/>' +
'<input type="text" name="levels[]"/>' +
'<input type="number" name="prices[]"/>');
Also I have another text box which should show the total price instantly. So I need to get the price data of each row after each row's price is entered by user. I tried to add an event to all of my prices[] fields like this:
$(function () {
$('input[name="prices[]"]').blur(function () {
alert('testAlert');
});
});
But it didn't work. I also tried to add an onblur event to my existing and generated input fields to get the prices like this:
<input type="number" name="prices[]" onblur="calculatePrice()">
with the function:
function calculateTotal() {
alert('testAlert');
var prices = $('input[name="prices[]"]').map(function () {
alert(this.value);
return this.value;
}).get();
}
I also tried specifying input field like this: 'input[name^="prices"]' and adding the event like this:
$('input[name="prices[]"]').on('blur', function() {
alert('testAlert');
});
And adding the event by class like this after I added class='priceClass' to my price input fields:
<input type="number" name="prices[]" class="priceClass">
$('.priceClass').on('blur', function() {
alert('testAlert');
});
None of these triggered the event. So how can I add an event to all elements of a HTML input array and get the all values in the input array after the event is triggered?
.onto a parent element that is not dynamically added, but specify the selector you wish to have trigger the event. ie.$('.container').on('blur','input[name="prices[]"]', function() { alert('testAlert'); });