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

239
Visualizações
Javascript: How to get ID of input in FormData?

I have a form of checkboxes. On submit, I get the FormData object and get access to the name and value of the selected checkboxes. However, I also need the unique ID identifier in order to properly POST the data to my backend. Looking through SO and the Mozilla docs, I have not found a way to get access to the ID property in addition to the name and value.

How do I get access to the id property on a selected input? Thanks for the help!

HTML

<form>
  <label>
    <input type="checkbox" name="customerResponse" value="Twitter" id="221"/>
    <span>Twitter</span>
  </label>
  <label>
    <input type="checkbox" name="customerResponse" value="IG" id="222"/>
    <span>IG</span>
  </label
  <label>
    <input type="checkbox" name="customerResponse" value="FB" id="223"/>
    <span>FB</span>
  </label
</form>

Javascript

function logSubmit(form) {

    let data = new FormData(form);
    let testarr = []
    for (const [name, value] of data) {
      if (value !="") testarr.push([{responseName: name},{customerResponse: value}])
    }

    console.log(testarr);
}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use element.id like this:

var idStr = element.id; // Get the id

MDN

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you can't use FormData to get the ID, but you can use below code:

$(form).find('input:checked').each((e, value) => {
                console.log(value.id);
                //Here you can use value.id 
            });
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