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

192
Visualizações
Getting values from checkboxes - javascript

I have a problem getting values from checkbox fields, I tried this method but it only works on radio type fields.

function writee(){
    var x = document.forms["formz"].music.value
    oo=window.open(" ","tab","width=300,height=350")
    oo.document.open()
    oo.document.write(x)
}
<form name="formz">
    What music do you prefer:
    <input type="checkbox" name="music" value="rock" > Rock
    <input type="checkbox" name="music" value="techno"> Techno
    <input type="checkbox" name="music" value="pop"> Pop
</form>
<button onclick="writee()">button</button>

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

function writee(){
    var markedCheckbox =document.querySelectorAll('input[type="checkbox"]:checked');
  let checkedValues = '';
    for (var checkbox of markedCheckbox) {
        checkedValues += checkbox.value + ' ';
    }
  
  console.log(checkedValues);
  // oo=window.open(" ","tab","width=300,height=350")
  // oo.document.open()
  // oo.document.write(x)
}
<form name="formz">
    What music do you prefer:
    <input type="checkbox" value="rock" > Rock
    <input type="checkbox" value="techno"> Techno
    <input type="checkbox" value="pop"> Pop
</form>
<button onclick="writee()">button</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

  function writee() {
    var checkboxes = Array.from(document.forms["formz"].music);
    var checked = checkboxes.filter((item) => item.checked);
    const values = checked.map((item) => item.value);
    console.log(values);
  }
<form name="formz">
  What music do you prefer:
  <input type="checkbox" name="music" value="rock" /> Rock
  <input type="checkbox" name="music" value="techno" /> Techno
  <input type="checkbox" name="music" value="pop" /> Pop
</form>
<button onclick="writee()">button</button>

I just modified it a little bit.

  1. Get all the related checkbox from form using form key
  2. Convert them to Javascript array using Array.from() so that we can use array methods
  3. Filter all the checkbox that are checked
  4. Get the values of checked checkbox and log it. (you can use these values according to your own need

i hope this will answer the question

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