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

221
Visualizações
Add div onclick with javascript

I am completely new to Javascript (and Stack Overflow) and I want to build a blog that allows the user to create a div onclick. I referenced this article on w3shool and tried all the three methods listed. However, the div doesn't show up when I use the first two methods and appears without clicking when I use the third one.
Here's my code for the third method:

HTML

<button class="box4" id="newEntry">NEW</button>

Javascript

const parent = document.getElementById("entries");

document.getElementById("newEntry").addEventListener("click", newEntry);

function newEntry() {
    var newDiv = document.createElement("div");
    parent.appendChild(newDiv);
}

For the second method, I replaced line 2 with

document.getElementById("newEntry").onclick= function () {newEntry()};

I have seen similar questions, but none of them solved my problem, so I would really appreciate your help.

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

0

There's nothing wrong with you code. It works fine. just one thing you have to remember: Div by default have no height, so if you want make sure to visualize it, just add height and color to it, like the code bellow.

const parent = document.getElementById("entries");
parent.style.width = '150px';
parent.style.height = '150px';
parent.style.backgroundColor = 'red';

document.getElementById("newEntry").addEventListener("click", newEntry);

function newEntry() {
    var newDiv = document.createElement("div");
    newDiv.style.width = '50px';
    newDiv.style.height = '50px';
    newDiv.textContent = 'Entry';
    newDiv.style.backgroundColor = 'white';
    parent.appendChild(newDiv);
}
<div id="entries"></div>
<button id="newEntry">new Entry</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is an example:

You can use querySelectorAll with a css selector to query all the elements matching the selector, but you can use the getElementByName too. In your example you query the elements by ID, but I don't se where you set the ids.

function onInitClick(){
  const buttons = document.querySelectorAll('button');
  const secondButton = buttons[1];
  secondButton.addEventListener('click', createButtonAndAppend);
  secondButton.innerText = 'Click me';
}

function createButtonAndAppend(){ 
     const button = document.createElement('button');
     button.addEventListener('click', createButtonAndAppend);
     button.innerText = 'Adds another button';
     document.body.append(button);
}
<button onclick="onInitClick();">Add click to second button</button> <button>Nothing happens</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

you can do it simply using jquery.

if you want to add something to div,

$("#your_div_id").html("your child element")

for example

<div id="example"></div>

$("#example").html("<h1>Hello world</h1>")

then result is

<div id="example"><h1>Hello world</h1></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