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

147
Vistas
How to makes div with a paragraph button and other elements inside it in HTML/JS

I have a button that creates a div, that but is easy (const smth = document.createElemnt(div);), but the only problem is it is just a blank div. I would like there to be a div with a paragraph and button in it. How do I do that? This is in HTML and JS.

I have tried to do:

(document.CreateElement(“p”);
document.CreateElement(“button”))

Separately but that isn’t on the div.

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

0

I hope you are well If I understand what you mean You want to create a div and add it to that element In JavaScript, you must first create the same div, then use appendChild to add other elements such as p and button.

const divMain = document.querySelector('#main-container')
        const div = document.createElement('div');
        const p = document.createElement('p');
        // p.classList = .....
        // p.textContent = .......
        div.appendChild(p);
        divMain.append(div);
        console.log(divMain)
<div id="main-container"></div>

Now to create the button, do exactly the same thing we did to create the p tag

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to append elements

let counter = 0;

document.querySelector('.container').addEventListener('click', function (event) {
  if (event.target instanceof HTMLButtonElement) {
    const div = document.createElement('div');
    const button = document.createElement('button');
    const p = document.createElement('p');
    
    button.textContent = 'Add';
    p.textContent = `${++counter} element created`;

    div.classList.add('new');
    div.append(button, p);
    
    this.append(div);
  }
});
.new {
  border: 1px solid green;
}

.new p {
  display: inline-block;
}
<div class="container">
  <button>Add</button>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

here is answer with jquery

$(document).ready(function(){
  $("#mybtn").click(function(){
  var myhtml=`
  <p>hello world</p>
  <button>hello!</button>
  `;
    $("#mydata").html(myhtml);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="mybtn">
click me!
</button>

<div id="mydata" style="background-color: lightblue">

</div>

if you don't want to use jquery than go for jquery to javascript converters like this

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