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

344
Visualizações
Adding a div to a page via a button in a different page

I am using HTML, CSS, and JavaScript to create a blog. There are 2 pages involved; a page where the posts are displayed, and a page where the posts are made. There is a "post" button in the latter (the page where the posts are made). I want that when the user clicks "post" after writing his or her post a div is added to the former (page where posts are displayed) with the content of the post (innerHTML of the textarea where the post is written).

I have used the following JavaScript code after watching a couple of videos but it is not working.

const BtnAdd = document.getElementById("postButton");
var cWrapper = document.getElementById("contentwrapper");

BtnAdd.addEventListener("click",AddNew);
function AddNew(){
    const newDiv = document.createElement("div");
    newDiv.classList.add("div-shadow");
    newDiv.innerHTML = document.getElementById("postcontent").innerHTML;
    cWrapper.appendChild(newDiv);
}

"cWrapper" refers to the location (div) where I want the divs to be added (in the page where the posts are displayed).

whenever I click the button with ID "postButton" no divs are added to the other page. Any ideas where I went wrong?

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

0

     //you need to add below code in page 1.
            const BtnAdd = document.getElementById("postButton");
            BtnAdd.addEventListener("click", AddNew);
            function AddNew() {
                var newhtml = document.getElementById("bannerText").innerHTML;
                sessionStorage.setItem("page1content", newhtml);
            }
    
    //you need to add below code in page 2.
            const newDiv = document.createElement("div");
            newDiv.classList.add("div-shadow");
            newDiv.innerHTML = sessionStorage.getItem("page1content");
            var cWrapper = document.getElementById("contentwrapper");
            cWrapper.appendChild(newDiv);


you have to add code two differnt page (page-1 like blog page, and page-2 like blog details page)
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