I have two scripts running on my page and my data is held within a table.
1) on button click - function addRownew - this adds a new row the the table containing select and input boxes.
2) The select box has a script that when changed will pass the value into a input box (and output to log)
Both scripts work ok as intended when kept apart, but when I use the .change function from within the dynamically created row, nothing happens - not even a log.
My script for the function addRownew
function addRownew(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
//alert(newcell.childNodes);
newcell.childNodes[0].value = rowCount;
}
}
Script for the change:
$(document).ready(function()
{
$(".productcode").change(function () {
console.log ('log onchange')
var result= $(this).val();
alert(result);
$('#unit_cost').val($('.productcode').val());
});
});
And the php table:
<TABLE id="dataTable" border="0">
<TR style="display:none;">
<TD width="5%" align="left"><INPUT type="checkbox" name="chk2"/></TD>
<TD width="150" align="left"><INPUT name="furn_id[]" type="hidden" value="0"/><INPUT name="furn_item[]" type="text" /></TD>
<TD width="150" align="left"> <select name="furn_room[]" multiple="MULTIPLE" >
<?php
$query=mysql_query("SELECT room FROM dbRooms ORDER BY room");
while($entry=mysql_fetch_array($query)) {
echo '<option value="' . $entry['room'] . '">' . $entry['room'] . '</option>';
} ?>
</select></TD>
<TD width="250" align="left"> <select name="furn_room[]" id="productcode" class="productcode" >
<option value="" ></option>
<?php $query=mysql_query("SELECT * FROM dbProducts ORDER BY product_name");
while($entry=mysql_fetch_array($query)) {
echo '<option value="' . $entry['product_code'] . ' - ' . $entry['product_name'] . ',' . $entry['product_unit'] . '">' . $entry['product_code'] . ' - ' . $entry['product_name'] . '</option>';
} ?>
</select></TD>
<TD width="50" align="left"> <INPUT name="furn_qty[]2" type="text" value="" size="2"/> <select name="furn_room[]" >
<?php $query=mysql_query("SELECT * FROM dbSizeOptions order by id");
while($entry=mysql_fetch_array($query)) {
echo '<option value="' . $entry['size'] . '">' . $entry['size'] . '</option>'; } ?>
</select></TD>
<TD align="left"> <INPUT name="furn_qty[]" type="text" value="" size="2"/></TD>
<TD width="50" align="left" valign="middle"> <INPUT name="furn_cost[]" type="text" value="" size="5"/></TD>
<TD width="50" align="left" valign="middle">£<INPUT name="unit_cost[]" id="unit_cost" class="unit_cost" type="text" value="" size="5"/></TD>
<TD width="50" align="left" valign="middle">£<INPUT name="furn_cost[]" type="text" value="" size="5"/></TD>
</TR>
</TABLE>