I have table that contains some select drop downs. Initially the table contains only one select drop down named 'item_code'. There is button named 'Add More' which will add more select button in that table with same named 'item_code'. I want display an alert message using jquery function on the onchange event of the select drop down... My problem is I am able to access the static select drop down in jquery function, but unable to access the dynamic select drop downs. Jquery function is given below-
$(function () {
var theValue;
$('select').focus(function () {
theValue = $(this).val();
});
$('select[name="item_code"]').change(function () {
alert(theValue);
});
})
and my html code is given below-
<div style="height:240px; overflow:auto">
<table id="dataTable" name="dataTable" border="1" bgColor="#9999CC" align="center" cellspacing="0" cellpadding="5">
<tr>
<td>Item Name</td>
<td>
<select name="item_code">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</td>
</tr>
</table>
<center>
<INPUT type="button" id="add_items" value="Add Items" onClick="addRow('dataTable')" />
<INPUT type="button" id="delete_items" value="Delete Items" onClick="deleteRow('dataTable')" />
</center>
</div>
and the javascript function for adding and deleting rows are given below
function addRow(tableID) {
var table = document.getElementById(tableID);
if (typeof addRow.s_no == 'undefined') {
addRow.s_no = 2;
}
else {
addRow.s_no++;
}
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[1].cells[i].innerHTML;
switch (newcell.childNodes[0].type) {
case "text":
if (i == 1) {
newcell.childNodes[0].id = "s_no" + (rowCount - 1);
newcell.childNodes[0].value = addRow.s_no;
} else{
newcell.childNodes[0].value = "";
}
break;
case "checkbox":
newcell.childNodes[0].checked = false;
break;
case "select-one":
// alert("i......"+i);
if(i == 3){
//newcell.childNodes[0].name = "item_code";
newcell.childNodes[0].id = "item_code" + addRow.s_no;
newcell.childNodes[0].selectedIndex = 0;
}else
{
newcell.childNodes[0].id = "item_category_code" + addRow.s_no;
newcell.childNodes[0].selectedIndex = 0;
break;
}
}
}
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for (var i = 0; i < rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if (null != chkbox && true == chkbox.checked) {
if (rowCount <= 2) {
alert("Cannot delete all the rows.");
break;
}
table.deleteRow(i);
rowCount--;
i--;
}
}
var count = 1;
for (var row = 0; row < addRow.s_no; row++)
{
// alert(addRow.s_no);
// document.getElementById(id).id="s_no"+row;
var id = "s_no" + row;
if (document.getElementById(id) != null) {
document.getElementById(id).value = count;
// alert("count=="+count);
count++;
}
}
addRow.s_no = count - 1;
} catch (e) {
alert(e);
}
}
Can anybody give a solution for this problem