I want to auto populate table based on the drop-down selection using javascript and input must be from array.
Asked
Active
Viewed 460 times
-2
-
1Welcome to stack overflow. I'd recommend taking a look at [how to ask a good question](https://stackoverflow.com/help/how-to-ask) to avoid any downvotes or flags. To be able to help you, we need to be able to see your code in written format, otherwise it's hard to give an answer without speculating. For this matter, here's an article on how to create a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your app. If you need any more help feel free to ask. – Dec 27 '19 at 15:07
-
Does this answer your question? [Populate form text field from database based on dropdown field selection](https://stackoverflow.com/questions/19822704/populate-form-text-field-from-database-based-on-dropdown-field-selection) – Nirav Patel Dec 27 '19 at 15:12
1 Answers
0
Maybe like this:
function chsnge_my_select(){
var select = document.getElementById('my_select');
var cell1_text = select.options[select.selectedIndex].value;
var cell2_text = select.options[select.selectedIndex].text;
var data_arr = [cell1_text, cell2_text];
add_to_my_tbl(data_arr);
}
function add_to_my_tbl(_arr){
if(!_arr[0] || !_arr[1]){
return false;
}
var table=document.getElementById('my_tbl');
var row = document.createElement('tr');
var cell1 = document.createElement('td');
var cell2 = document.createElement('td');
cell1.innerHTML = _arr[0];
cell2.innerHTML = _arr[1];
row.appendChild(cell1);
row.appendChild(cell2);
table.appendChild(row);
}
document.getElementById('my_select').addEventListener('change', chsnge_my_select, false);
<select id="my_select">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
</select>
<table id="my_tbl" width="100%" border="1">
<tr>
<td>id</td>
<td>name</td>
</tr>
</table>

mscdeveloper
- 2,749
- 1
- 10
- 17