i am trying to change the value of dynamically generated input field.
the scenario:
i have a button called add items
, when clicked it append three input fields named as itemname
,units
and unitprice
of which itemname
has a jqueryui autocomplete
and working great
, but i am also listing keypress event
of itemname field
so when user press enter after selecting required itemname from autocomplete it should get units and price of the itemname which user has selected using autocomplete the code:
var count = 1;
$('#add-items').click(function () {
$('#items').append('<div><br><i class="fa fa-angle-right"></i>. <input type="text" name="itemname[]" id="itemname-' + count + '" class="itemname" placeholder="item name"/>\n\
<input type="text" name="units[]" id="units-' + count + '" size="10" placeholder="units" style="text-align: right"/>\n\
<input type="text" name="unitprice[]" id="unitprice-' + count + '" size="10" placeholder="unit price" style="text-align: right"/>\n\
<a href="javascript:void(0);" id="remove-items"><i class="fa fa-trash-o"></i></a></div>');
$('#itemname-' + count).autocomplete({
source: "<?= site_url('administrator/item-autocomplete') ?>",
minLength: 2
});
$('#itemname-' + count).on('keypress', function (e) {
var name = jQuery(this).val();
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
e.preventDefault();
$.ajax({
type: "POST",
url: "<?= base_url() ?>administrator/get-unit-and-price/",
data: {name: name},
dataType: "json",
success: function (data) {
if (data.valid == true) {
//document.getElementById('units-' + count).value = data.unit;
//document.getElementById('unitprice-' + count).value = data.price;
$('#units-' + count).attr('value', data.unit);
$('#unitprice-' + count).attr('value', data.price);
//$('#units-' + count).val(data.unit);
//$('#unitprice-' + count).val(data.price);
} else {
alert(data.html);
}
}
});
}
});
count++;
});
everything working great, but its not changing the value of unit and unitprice value, i have also tried alerting the data.unit
and data.price
and its alerting the proper value from database..
i am wondering what i need to do more so that it works.. looking for suggestion and help, thanks in advance.. sorry for bad english..
posting the solution so that others might get some help... :)
::::::::::::::::::::::::::EDIT:::::::::::::::::::::::::::::::
addedd data-count
to the dynamic input field itemname[]
and modified the success function of ajax:
$('#units-' + thecount).attr('value', data.unit);
$('#unitprice-' + thecount).attr('value', data.price);
now things are working and when i press enter it also changes the value of units[]
and unitprice[]
input field,
::::::::::::::updated solution::::::::::::::::
var count = 1;
$('#add-items').click(function () {
$('#items').append('<div><br><i class="fa fa-angle-right"></i>. <input data-count="'+ count +'" type="text" name="itemname[]" id="itemname-' + count + '" class="itemname" placeholder="item name"/>\n\
<input type="text" name="units[]" id="units-' + count + '" class="units" size="10" placeholder="units" style="text-align: right"/>\n\
<input type="text" name="unitprice[]" id="unitprice-' + count + '" class="unitprice" size="10" placeholder="unit price" style="text-align: right"/>\n\
<a href="javascript:void(0);" id="remove-items"><i class="fa fa-trash-o"></i></a></div>');
$('#itemname-' + count).autocomplete({
source: "<?= site_url('administrator/item-autocomplete') ?>",
minLength: 2
});
$('#itemname-' + count).on('keypress', function (e) {
var name = jQuery(this).val();
var thecount = jQuery(this).data('count');
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
e.preventDefault();
$.ajax({
type: "POST",
url: "<?= base_url() ?>administrator/get-unit-and-price/",
data: {name: name},
dataType: "json",
success: function (data) {
if (data.valid == true) {
$('#units-' + thecount).attr('value', data.unit);
$('#unitprice-' + thecount).attr('value', data.price);
} else {
alert(data.html);
}
}
});
}
});
count++;
});
thanks to every one....