Tell me how to set the arr array to value (select-> option-> value). And the user should only output text that contains an array () array. Simply put, array shows text between tags.
and the value of the array is arr

Tell me what you need to fix in the code so that it is:

<select > <option value="0"></option> <option value="1">Замечание</option> <option value="2">согласовано</option> <option value="3">не согласовано</option> </select> 

at the moment it turns out only this way:

 <select > <option value=""></option> <option value="Замечание">Замечание</option> <option value="согласовано">согласовано</option> <option value="не согласовано">не согласовано</option> </select> 

Code

 var array = [" ", "Замечание", "согласовано", "не согласовано"]; //var arr=["0", "1", "2", "3"]; как разместить данный массив в value option var select; select=document.createElement('select'); select.name=this.dataset.name; select.dataset.id=this.dataset.id; select.type='text'; var val=this.innerHTML; select.value=this.innerHTML; this.dataset.old=this.innerHTML; this.innerHTML=''; for (var i = 0; i < array.length; i++) { var option = document.createElement("option"); option.setAttribute("value", array[i]); option.text = array[i]; select.appendChild(option); } select=this.appendChild(select); 
  • 3
    replace option.setAttribute("value", array[i]); on option.setAttribute("value", i); ? - teran

2 answers 2

Create an array of objects containing name and value.

 var array = [ {name:" ", value:0}, {name:"Замечание", value:1}, {name:"согласовано", value:2}, {name:"не согласовано", value:3333} ]; var select; select=document.createElement('select'); select.name=this.dataset.name; select.dataset.id=this.dataset.id; select.type='text'; var val=this.innerHTML; select.value=this.innerHTML; this.dataset.old=this.innerHTML; this.innerHTML=''; for (var i in array) { var option = document.createElement("option"); option.setAttribute("value", array[i].value); option.text = array[i].name; select.appendChild(option); } select=this.appendChild(select); 

  • The code works as it should, tell me how to be in the following situation. My select is dynamic, and when I click on a cell in the table, select appears with values ​​(Remark) and so on, as soon as the focus of the created object is lost, the value is added to the cell (everything is correct here). But value is added to the database. I just thought that the text would remain between the <option> </ option> tags, but it turned out differently. - Eliot
  • one
    @Eliot Apparently, this comment draws on another question. - ilyaplot
  • I thought I could briefly explain) - Eliot
  • @Eliot I couldn’t even understand the question. If you understand correctly now, then on the server side you need to create a table with values ​​and add IDs, and when retrieving data, associate tables. - ilyaplot
 var array = [" ", "Замечание", "согласовано", "не согласовано"]; var select = document.createElement("select"); array.forEach(function(v,k){ var option = document.createElement("option"); option.value = k; option.innerHTML = v; select.appendChild(option); }); 
  • one
    In vain minusanuli, suddenly my friend wants a not so flat value ? - vp_arth
  • @vp_arth then it is more logical to create an object from pairs - keys and values, and then it turns out to be a crutch. - SLy_huh
  • @SLy_huh I did as the question was asked. perhaps the second array is taken from the side and he needs it that way. but since he accepted the couples option, I edited my answer. - webDev_
  • I am very happy, but why make an array in an array, if there is an object for this? xzibit ? - SLy_huh
  • @Slyyhuh sense if the values ​​are only 2? as for me it is unnecessary to attribute keys as strings when it is known that more than 2 values ​​will not be there - webDev_