Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
How to show the selected option after selecting the option

I have a form with table structure html like below:

                             <tr>
<td>Quotation Category<span class="required" style="color: red">*</td>
<td>
    <div class="form-group col-md-9">
        <select name="quotation_category" class="form-control" style="width: 80%;" id="quotation_category">
            <option value="">Pilih Kategori</option>
            <option value="konvensional">Konvensional</option>
            <option value="syariah">Syariah</option>
        </select>
    </div>
</td>
<td class="ujroh-style">Ujroh<span class="required" style="color: red">*</span>
</td>
<td class="ujroh-style">
    <div class="form-group col-md-9">
        <input type="number" class="form-control" id="ujroh" name="ujroh">
    </div>
</td> </tr>

Below is my js:

document.querySelector('#quotation_category').addEventListener('change', () => {
        const select_value = document.querySelector('#quotation_category').value
        if (select_value === "konvensional") {
            document.getElementsByClassName('.ujroh-style').style.display = "none"
        } else if (select_value === "syariah") {
            document.getElementsByClassName('.ujroh-style').style.display = "revert"
        }
    })

I want, if I choose 'konvensional', that that selected option Ujroh will appear. If I choose 'syariah', then the selected option Ujroh dissapears. How to do that?

INFORMATION
My code gives this error:

Uncaught TypeError: Cannot set properties of undefined (setting 'display')
at HTMLSelectElement.<anonymous>
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This piece of line is causing error

document.getElementsByClassName('.ujroh-style').style.display = "some-value"
  1. You don't need to use . in front because you are already selecting by className

  2. You are using getElements note the s it's plural if that's your intention you need to loop through all the elements and change the display property on the element not on the array

const elems = Array.from(document.getElementsByClassName('ujroh-style'))
elems.forEach(el => el.style.display = 'your-value')
about 4 years ago · Juan Pablo Isaza Relatório

0

Three problems:

  1. you are trying to get the style property from a collection of elements, and a collection of elements does not have the style property. Only a single element has a style property. Thankfully, you can iterate through each element
  2. you are trying to get all elements of a particular class, with a string that has the . on it, which will get you an empty collection. Remove the .
  3. revert is not a valid CSS display property enum. Use something else

So your previous JavaScript code would now be:

document.querySelector('#quotation_category').addEventListener('change', () => {
    const select_value = document.querySelector('#quotation_category').value
    if (select_value === "konvensional") {
        for (const el of document.getElementsByClassName('ujroh-style')) {
            el.style.display = "none"
        }
    } else if (select_value === "syariah") {
        for (const el of document.getElementsByClassName('ujroh-style')) {
            el.style.display = "inline-block"
        }
    }
})
about 4 years ago · Juan Pablo Isaza Relatório

0

One problem is that you use '.classname' as selector in the getElementsByClassName selector. And second you are trying to add a style to a collection of elements, that is not possible. To fix this, you have to add the display style to every element in the collection.

document.querySelector('#quotation_category').addEventListener('change', () => {
  const select_value = document.querySelector('#quotation_category').value
  if (select_value === "konvensional") {
    document.querySelectorAll('.ujroh-style').forEach(x => x.style.display = 'none');
  } else if (select_value === "syariah") {
    document.querySelectorAll('.ujroh-style').forEach(x => x.style.display = 'inline-block');
  }
})
<table>
  <tr>
    <td>Quotation Category<span class="required" style="color: red">*</span></td>
    <td>
      <div class="form-group col-md-9">
          <select name="quotation_category" class="form-control" style="width: 80%;" id="quotation_category">
              <option value="">Pilih Kategori</option>
              <option value="konvensional">Konvensional</option>
              <option value="syariah">Syariah</option>
          </select>
      </div>
    </td>
    <td class="ujroh-style">Ujroh<span class="required" style="color: red">*</span>
    </td>
    <td class="ujroh-style">
      <div class="form-group col-md-9">
          <input type="number" class="form-control" id="ujroh" name="ujroh">
      </div>
    </td> 
  </tr>
</table>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda