I am using dynamic rows in combination with the onChange function. The first row is manually written in HTML and the other rows are created in Javascript.
I am using the count function to define the ID in the dynamic rows.
Here is an example of the rows that are manually written in HTML:
<div id="asdasd123">
<div id="row_id_1">
<div>
<select id="plusmin1" name="plusmin[]" onChange="getVers1(this.value)" data-srno="1">
<option selected="disabled" value="0"> </option>
<option value="1">Data one</option>
<option value="2">Data two</option>
<option value="3">Data three</option>
</select>
</div>
<div class="form-group">
<input type="text" name="totalprice[]" id="totalprice1" data-srno="1">
</div>
</div>
<script>
function getVers1() {
var numVal1 = Number(document.getElementById('plusmin1').value);
var totalValue = numVal1 * 100
document.getElementById('totalprice1').value = totalValue.toFixed(2);
}
</script>
<input type="text" name="total_item" id="total_item" value="1" />
<button type="button" name="add_row" id="add_row" class="btn btn-success">New row</button>
When you click on add_row the script add`s creates new rows with the following script:
<script>
$(document).ready(function(){
var count = 1;
$(document).on('click', '#add_row', function(){
count++;
$('#total_item').val(count);
var html_code = '';
html_code += '<div id="row_id_'+count+'">';
html_code += '<div><select id="plusmin'+count+'" name="plusmin[]" data-srno="'+count+'"><option selected="disabled" value="0"> </option><option value="1">Data one</option><option value="2">Data two</option><option value="3">Data three</option></select></div>';
html_code += '<div class="form-group"><input type="text" name="totalprice[]" id="totalprice'+count+'" data-srno="'+count+'"></div></div>';
$('#asdasd123').append(html_code);
}
});
$(document).on('change', '[id^=plusmin]', function selectQuantity(selectedValue){
var numVal2 = Number(document.getElementById('plusmin'+count+'').value);
var totalValue2 = numVal1 * 100
document.getElementById('totalprice'+count+'').value = totalValue2.toFixed(2);
});
});
</script>
I have a problem with the count
in my script.
When I have just one row in my screen and change the value of plusmin1
the script runs the onchange verry well.
When I have two rows in my screen and change the value of plusmin2
the script also works. But when I change the value of plusmin1
the script changes totalprice2
. Which is a textbox that I use for the second row.
Does someone know the reason for that? Why is the script changing the value for the second textbox when I change the value for the first row?