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

215
Visualizações
Passing a dynamically created variable to another function (Vanilla Javascript)

I have updated my question to include the code. The "updateNewP" function is not updating the innerHTML but it is not throwing an error. Do I need to add "this" somewhere in both of these functions? Javascript:

let initial = document.getElementById("test");
initial.addEventListener("click", newPElement);

let update = document.getElementById("update");
update.addEventListener("click", updateNewP);

let myTextArea = document.getElementById("textArea");

//function that creates a P
function newPElement(){
let newP = document.createElement("p");
newP.classList.add("myFont");
newP.innerHTML = "Initial Text";
myTextArea.appendChild(newP);
return newP;
}

function updateNewP(newP){
    newP.innerHTML ="Updated";
}

HTML:

<h1>Testing</h1>
    <button id="test" class="test">Test</button>
    <button id="update">Update</button>
    <section id="textArea">
    </section>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The newP variable that was declared within functionA is only available in functionA. If you need to work with this variable in a newPUpdate, it needs to be passed as an argument:

function functionA() {
  const newP = document.createElement("div");
  newPUpdate(newP);
}

function newPUpdate(elem) {
  elem.innerHTML = `${someVariable}`;
}

Looking up variable scope in javascript will help you understand what's going on.

about 4 years ago · Juan Pablo Isaza Relatório

0

The addEventListener passes event to the function. Event does not have the innerHTML key because it is not an element.

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