Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
Create form elements

I'm trying to create a form where a user is able to fill his full name in inputs. I've created a simple form, but the form shows only first name input. I'm thinking about forEach method to fulfill the task, but don't correctly understand how to make it. Or should I look into another way? I've also thought about creating 'second-name' and 'third-name' elements but this way doesn't fit me. Thanks

const nameInput = document.querySelector('.name');
const button = document.querySelector('.button')
const lists = document.querySelector('.lists')

function addName(evt) {
    evt.preventDefault();
    const listItem = document.createElement('li');
    listItem.textContent = nameInput.value;
    lists.appendChild(listItem);
    nameInput.value = '';
}

button.addEventListener('click', addName);
    <form class="form">
        <input type="text" class="name" placeholder="Имя">
        <input type="text" class="name" placeholder="Фамилия">
        <input type="text" class="name" placeholder="Отчество">
        <button type="submit" class="button">ОК</button>
    </form>
    <div class="block">
        <ul class="lists"></ul>
    </div>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you want to take the names from the form and make a list item of them every time the button is pressed, you can use querySelectorAll to select all the name inputs and loop through them to get the names.

const nameInputs = document.querySelectorAll('.name');
const button = document.querySelector('.button')
const lists = document.querySelector('.lists')

function addName(evt) {
    evt.preventDefault();
    const listItem = document.createElement('li');
    var names = []
    nameInputs.forEach(i =>{
      names.push(i.value);
      i.value = '';
    });
    listItem.textContent = names.join(' ');
    lists.appendChild(listItem);
}

button.addEventListener('click', addName);
<form class="form">
        <input type="text" class="name" placeholder="Имя">
        <input type="text" class="name" placeholder="Фамилия">
        <input type="text" class="name" placeholder="Отчество">
        <button type="submit" class="button">ОК</button>
    </form>
    <div class="block">
        <ul class="lists"></ul>
    </div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

It is a good practice to always use name attribute for your form elements. So that you can identify them.

Simply add name to your inputs and give an id to your form.

  const nameInput = document.querySelector('.name');
const button = document.querySelector('.button')
const lists = document.querySelector('.lists')



function addName(evt) {
    evt.preventDefault();
    const contact_form = document.getElementById("contact_form");
    
    Array.from(contact_form.elements).forEach((input) => {
         if(input.type === 'text') {
          const listItem = document.createElement('li');
          listItem.textContent = `${input.name} = ${input.value}`;
          lists.appendChild(listItem);
          input.value = '';
         }
    });
}

button.addEventListener('click', addName);




 <form id="contact_form" class="form">
        <input type="text" name="firstName" class="name" placeholder="Имя">
        <input type="text" name="lastName" class="name" placeholder="Фамилия">
        <input type="text" name="email" class="name" placeholder="Отчество">
        <button type="submit" class="button">ОК</button>
    </form>
    <div class="block">
        <ul class="lists"></ul>
    </div>

https://jsfiddle.net/rjv32dzt/

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda