I'm trying to have a select box with a value that shows based on if the database has a "yes" or "no" in the column. I cannot figure out what my error is in my syntax, but this will not work. I am using JavaScript values to fill all of my table.
tbl +='<td ><select name="status"><option'+if (val['Col4'] == "yes"): + 'selected>yes</option><option'+if (val['Col4'] == "no"): +'selected>no</option></select></td>';
Here is my table:
tbl +='<tr row_id="'+row_id+'" style="background color:'+val['default_color']+'">';
tbl +='<td ><div col_name="Col1">'+val['Col1']+'</div </td>';
tbl +='<td ><div col_name="Col2">'+val['Col2']+'</div></td>';
tbl +='<td ><div col_name="Col3">'+val['Col3']+'</div></td>';
tbl +='<td ><select name="status"><option'+if (val['Col4'] == "yes"): + 'selected>yes</option><option'+if (val['Col4'] == "no"): +'selected>no</option></select></td>';
tbl +='<td >;
tbl +='</tr>';
But my table crashes.
The PHP is working for all the other parts of the table. It's getting through an ajax request and then saving the value.
So val['Col4']
will work as a text input, but not as a select box selection.
Use a conditional (aka "tertiary") expression. You need to add both options, not a single option with a conditional in it. Then each option conditionally adds selected
.
tbl +='<td ><select name="status">';
tbl += '<option' + (val['Col4'] == "yes" ? ' selected' : '') + '>yes</option>';
tbl += '<option' + (val['Col4'] == "no" ? ' selected' : '') + '>no</option>';
tbl += '</select></td>';
You were also missing a space between option
and selected
.