Problems using select in javascript

José Carlos 886 Reputation points
2024-04-13T14:35:14.8333333+00:00

Hi friends,

I have the following problem. I have 8 selects within a div, where each select has two values: 0 and 1. When I click for the first time on any select to select 0 or 1, when I click on the select arrow, it already considers that I selected 0. From then on, when clicking on the arrow for other selects, the functionality is normal. Only the first time, it behaves differently. Follow the code.

Tks

  const divElementos = document.getElementById('custom-select');

  const selectElementos = divElementos.querySelectorAll('select');

  // cliue nos selects
  divElementos.addEventListener('click', function() 
  {
          
    selectElementos.forEach(select => 
      {
      binaryCode = '';
      total = 0;
      let t;
      let cont = 0;

     // varre todos os selects
      selectElementos.forEach(select => 
      {
        
        valorSelecionado = obterValorSelecionado(select);
        idDoSelect = select.options[select.selectedIndex].value;
        
            if(idDoSelect == 1)
            {
                var t = binaryValues[cont];
                total = total + t;
            }
            binaryCode += valorSelecionado;
            document.getElementById('txt1').textContent = total + " em binário é: " + binaryCode;
            cont++;
      });
      });
      if (total == numsorteado) 
      {  
        // Mostra o botão com ID "jogardenovo"
        botao.style.display = 'inline';
        document.getElementById('msg').textContent = "Correto! Muito bem.";

      }    
  });

JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
871 questions
0 comments No comments
{count} votes