0

I have a select that filled with JSON data dynamically with the following data: Element, 1, 2, 3 ... once selected, the select is hidden since a button is shown again, but I can not show it with the Element value, it is shown with the value that the user chose, how could I change the value of the select from Javascript? 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 Select:

$('#boards')
  .append($("<option></option>")
  .attr("value",'')
  .text("Eelement")); 

$.each(boards, function(index, value) {
     $('#boards')
        .append($("<option></option>")
        .attr("value",value.id)
        .text(value.name)); 
     arrayBoards.push(value.id);
});

Javascript to change

function cambiar(){
     document.getElementById("tablaSelect").style.display="block";
     document.getElementById("select").value = '';
}

2 Answers 2

1

Your document.getElementById("select").value = ''; would work if there's an option with that value (and looking at your code, there is), but the id of the element is boards, not select:

$('#boards')
  .append($("<option></option>")
  .attr("value",'')
  .text("Eelement"));

var boards = [
  {id: 1, name: "One"},
  {id: 2, name: "Two"},
  {id: 3, name: "Three"},
  {id: 4, name: "Four"}
];
var arrayBoards = [];

$.each(boards, function(index, value) {
     $('#boards')
        .append($("<option></option>")
        .attr("value",value.id)
        .text(value.name)); 
     arrayBoards.push(value.id);
});

function cambiar(value){
     document.getElementById("tablaSelect").style.display="block";
     document.getElementById("boards").value = value;
}

cambiar("1");
setTimeout(function() {
  cambiar("");
}, 800);
<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>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Or, since you're using jQuery, use its val: $("#boards").val("");

$('#boards')
  .append($("<option></option>")
  .attr("value",'')
  .text("Eelement"));

var boards = [
  {id: 1, name: "One"},
  {id: 2, name: "Two"},
  {id: 3, name: "Three"},
  {id: 4, name: "Four"}
];
var arrayBoards = [];

$.each(boards, function(index, value) {
     $('#boards')
        .append($("<option></option>")
        .attr("value",value.id)
        .text(value.name)); 
     arrayBoards.push(value.id);
});

function cambiar(value){
     document.getElementById("tablaSelect").style.display="block";
     $("#boards").val(value);
}

cambiar("1");
setTimeout(function() {
  cambiar("");
}, 800);
<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>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

0

Yes You can change the value of dropdown dynamically like this

function cambiar(){
     document.getElementById("tablaSelect").style.display="block";
     $("#boards").val(Any value here);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.