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

204
Visualizações
Why creating a <p> and appending it into a <div> is not working

I am trying to simulate something like reddit where you can write a text and email and when you submit, they post in a "div". Somehow when I compiled it on stack overflows web compiler it console.logged the giving text but not as "p" inside a "div"

let submit = document.getElementById('submit');
let inputfield = document.getElementById('input-field');
let mail = document.getElementById('mail-field');
let posts = document.getElementsByClassName('post');

console.log(inputfield)

submit.addEventListener('click', function(){
    console.log(inputfield.value)
    var paragraph = document.createElement('p')
    paragraph.classList.add('paragraph-styling');
    paragraph.innerText = inputfield.value;
    posts.appendChild(paragraph);
    inputfield.value = "";
})
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Dom Manipulation</title>
</head>
<body>
    <script src="script.js"></script>

    <nav id="navbar">
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact</a></li>
        </ul>
    </nav>
    
    <h1>Welcome to This Page</h1>
    <div class = "inputs">
        <input id="mail-field" type="text" placeholder="Write your mail"> 

        <textarea id="input-field" placeholder="Write something"></textarea>

        <button id="submit">Submit</button>
    </div>

    <div class="post" style="width:50%; margin: 0 auto;">

    </div>

</body>
</html>

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

0

getElementsByClassName will return an array-like like objects which has post class. So you have to get elements from that array-like objects. You can use index here as

posts[0].appendChild( paragraph );

let submit = document.getElementById('submit');
let inputfield = document.getElementById('input-field');
let mail = document.getElementById('mail-field');
let posts = document.getElementsByClassName('post');


submit.addEventListener('click', function() {
  var paragraph = document.createElement('p')
  paragraph.classList.add('paragraph-styling');
  paragraph.innerText = inputfield.value;
  posts[0].appendChild(paragraph);
  inputfield.value = "";
})
<nav id="navbar">
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

<h1>Welcome to This Page</h1>
<div class="inputs">
  <input id="mail-field" type="text" placeholder="Write your mail">

  <textarea id="input-field" placeholder="Write something"></textarea>

  <button id="submit">Submit</button>
</div>

<div class="post" style="width:50%; margin: 0 auto;">

</div>

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