I am having a table. In every row, I will be having 5 input fields. There is a button addrow. when you click on it,it will create a new row.
addrow function(){
var table = document.getElementById("rtable");
var row = table.insertRow();
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3=row.insertCell(2);
var cell4=row.insertCell(3);
var cell5=row.insertCell(4);
var cell6=row.insertCell(5);
cell1.innerHTML="<td><input type='text' class='form-control' name='r1'></td>"
cell2.innerHTML="<td><input type='date' class='form-control' name='r2'></td>"
cell3.innerHTML="<td><input type='number' class='form-control' name='r3' ></td>"
cell4.innerHTML="<td><select class='form-control' name='r4'>"+
"<option value='yes' >Yes</option><option value='no' selected>No</option></select> </td>"
cell5.innerHTML="<td><input type='date' class='form-control' name='r5'></td>"
cell6.innerHTML="<td><button type='button' class='buttongreentab' onclick='remcosdeleterow(this)'>Delete</button> </td>"
}
now i want to add a functionality to 'r4'. i.e 4th cell. if my selected option is yes, then r5 should be enabled. if my selected option is No, then r5 should be disabled. but the problem is every row will be having a cell with name r5. I tried many options. Nothing worked. Can someone help me?
Based on suggestions, I edited my code as:
<script>
const getSelect = document.querySelectorAll('select.form-control');
getSelect.forEach(getSingleSelect => {
getSingleSelect.addEventListener('change', () => {
console.log(getSingleSelect.value);
if (getSingleSelect.value == 'yes') {
// console.log('I will render cell5.innerHTML');
cell5.innerHTML="<td style='display:none'><input type='date' class='form-control' name='os5'></td>"
} else {
console.log('I will not render cell5.innerHTML');
}
})
});
</script>
when I change my select option to yes, still there is no use. I dont understand where i am going wrong? I am having a table. there are 5 columns. and we can add / delete the rows based on our requirement. I want to add a feature so that when someone selects yes on 4th column, 5th column should be enabled and if 4th column is no, then 5th column should be disbaled. I dont know how to achieve this? Please help me.. :(