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

213
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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