I have a select that dynamically fills with data that I receive in a JSON, the user then chooses a value and continues using the application, I am trying that when I show that select again when pressing a button, it reappears with a default option that is defined like "Choose board" but I can't change the select from Javascript to show that option, I have this implemented HTML:
<div id="tablaSelect">
<table id=tabla2 style="width:80%" >
<tr>
<th>
<div class="styled-select">
<select class="select" id="boards">
</select>
</div>
</th>
</tr>
</table>
Javascript to fill the select: // Assemble the select with the boards
$('#boards')
.append($("<option></option>")
.attr("value",'')
.text("Elegir un tablero"));
$.each(boards, function(index, value) {
$('#boards')
.append($("<option></option>")
.attr("value",value.id)
.text(value.name));
arrayBoards.push(value.id);
});
Javascript to change the select:
function cambiar(){
document.getElementById("tablaSelect").style.display="block";
document.getElementById("select").value = '0'; //No se hace así porque no tengo values
}
Any idea how to make the select with the "Choose board" option appear when executing the change function??? Thanks a lot :)
Add the "selected" attribute when filling the select
For this you have to first put the
id
in the in the method to select elements you are puttingselect
and theid
isboards
and then it would be to do something like thisdocument.getElementById("boards").selectedIndex = 0
; but you also have to be clear about where you call the change function because I don't see in your code when you call it if you want it to be when you select an optionselect
you should do this<select class="select" onchange="cambiar();" id="boards"></select>
but if you want to put it in a button you just have to call the function in I hope the eventonclick="cambiar();"
will solve your answer