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

156
Vistas
How do I replace text inside a paragraph inside a div element?

I need to set the text within a Paragraph that is within a DIV element dynamically. I know the divID and can get the div by id using get document.getElementById(divID) This returns the following:

<div id="mynote72031" class="mydiv" ondrop="drop(event,this.id)" ondragover="allowDrop(event)" style="cursor: move; display: block; top: 19px; left: 19px; width: 375px;">
  <a style="top:0px; right:5px;  position:absolute; color:#F00" href="javascript:;" onclick="hideElement(this)">X</a>
  <p id="noteContent1" ondblclick="editContent(this)">Note Number: 1</p>
</div>

The function should look like this:

function updateNote(divID, paragraphInnerHTML) {
  var updateNoteP = document.getElementById(divID);
  //Update Paragraph inside the DIV here
}

Note that the id of the paragraph is always noteContent1

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

0

There are 2 ways you can change the text within HTML tag

function updateNote(divID, paragraphInnerHTML) {
  var updateNoteP = document.getElementById(divID);
  //Update Paragraph inside the DIV here
  let $targetEle = updateNoteP.childNodes[1]
  // use innerHTML
  $targetEle.innerHTML = paragraphInnerHTML
  // or textContext
  $targetEle.textContent = paragraphInnerHTML 
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use the Node.lastChild to access the p tag as it is the last element within the div tag

function updateNote(divID, paragraphInnerHTML) {
    var updateNoteP = document.getElementById(divID);
    //Update Paragraph inside the DIV here
    let pElement = updateNoteP.lastChild;
    pElement.innerHTML = paragraphInnerHTML;
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Pretty close with your variable name, this assumes that it's html you wanna use. If it's just a string you can use updateNoteP.innerText instead

If you pass in pID as "noteContent1" this will change the content of the P

function updateNote(pID, paragraphInnerHTML) {
  var updateNoteP = document.getElementById(divID);
  // check that element is found
  if (updateNoteP) {
  // update inner html
   updateNoteP.parentElement.children[1].innerHTML = paragraphInnerHTML
  }
}
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