Select multiple with dynamic operation

14

I have a select with 4 options. These 4 options are the number of options that will appear in a multiple select.

WhatIwantisassoonastheuserselectsthemostfrequentwordquantityoption,javascriptalreadyfillsthesecondselectwiththenumberofwordsselectedinthefirstselect.Forthis,thebiggestoptionthattheusercanputis20andthecodebehind(injava)hasalistwiththe20mostfrequentwords.Thatis,Ineedafunctioninjavascriptthattakesthevalueselectedinthefirstselect(X)andloadsthesecondselectwiththefirstXelementsofthelist(injava).

Forittoappearinselect,thecodelookslikethis:

<selectname="vet[]" size="4" multiple><%
   for(int i=0;i<5;i++){ %> <option> <% out.print(documento.vetor[i]); %> </option><%  }  %>
</select> <br>

In the case, the for is size 5 by default, but it is this number that I wish to change ..

    
asked by anonymous 27.01.2015 / 00:54

1 answer

9

Since% w / w% w / w% is the same as% w / w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w w round!

var lista = [];

pegaLista();

function pegaLista() {
  var disponiveis = document.getElementById("disponiveis");
  for (i=0; i < disponiveis.options.length; i++) {
    lista.push(disponiveis.options[i].text);
  }
  limpaLista(disponiveis);
}

function muda() {
  var maisAtivas = document.getElementById("maisAtivas");
  var selecionado = maisAtivas.options[maisAtivas.selectedIndex].text;
  
  var disponiveis = document.getElementById("disponiveis");
  limpaLista(disponiveis);
  
  for (var i=0; i < selecionado; i++) {
    var option = document.createElement("option");
    option.text = lista[i];
    disponiveis.add(option);
  }
}

function limpaLista(elemento) {
  while(elemento.options.length > 0) {
    elemento.remove(0);
  }
}
Lista das
<select id="maisAtivas" onchange="muda()">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>
palavras mais ativas.
<br><br>
<!-- Select semelhante ao seu com name='vet[]' que vem preenchido pelo java -->
<select id="disponiveis" multiple="true">
  <option>de - 5</option>
  <option>voce - 3</option>
  <option>que - 3</option>
  <option>isso - 2</option>
  <option>outra - 2</option>
</select>

JSFIDDLE

The select function runs as soon as you load the page. It will get the data of disponiveis that is already filled by java. After collecting the data it clears the select to be empty.

    
29.01.2015 / 11:28