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

245
Visualizações
How to set the "value" and "for" attribute using Object.assign

How do I set the value of an element using Object.assign()? All other attributes seem to show up except "value"for an <input> and "for" for a <label>. Here's the code:

window.onload = function createForm () {

    // Form header >>>>
    const formHeaderText = document.createTextNode("Form");
    headerEl.appendChild(formHeaderText);
    container.appendChild(headerEl);

    // Form element >>>
    Object.assign(formEl, {
        name: "inputForm",
        id: "myForm"
    });
   container.appendChild(formEl);

    // Name Label >>>
    const nameLabelText = document.createTextNode("Name");
    labelElName.appendChild(nameLabelText);
    Object.assign(labelElName, {
        for: "iname",
    });
    formEl.appendChild(labelElName);

    // Name Input element >>>
    formEl.appendChild(inputElName);
    Object.assign(inputElName, {
        className: "inputs",
        id: "iname",
        name: "name",
        type: "text",
        placeholder: "Name",
        value: ""
    })

And what I get is:

<input class="inputs" id="iname" name="name" type="text" placeholder="Name">

for the input and:

<label>Name</label>

for the label element.

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

0

Since for is a reserved keyword in JavaScript, you need to use htmlFor property on label element, like this:

Object.assign(labelElName, {htmlFor: "iname"});

or

labelElName.htmlFor = "iname";
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