Necesito configurar el texto dentro de un Párrafo que está dentro de un elemento DIV dinámicamente. Conozco el divID y puedo obtener el div por id usando get document.getElementById(divID) Esto devuelve lo siguiente:
<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>La función debería verse así:
function updateNote(divID, paragraphInnerHTML) { var updateNoteP = document.getElementById(divID); //Update Paragraph inside the DIV here }Tenga en cuenta que la identificación del párrafo siempre es noteContent1
Hay 2 formas de cambiar el texto dentro de la etiqueta HTML
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 }Puede usarNode.lastChild para acceder a la etiqueta p , ya que es el último elemento dentro de la etiqueta div
function updateNote(divID, paragraphInnerHTML) { var updateNoteP = document.getElementById(divID); //Update Paragraph inside the DIV here let pElement = updateNoteP.lastChild; pElement.innerHTML = paragraphInnerHTML; }Bastante cerca con su nombre de variable, esto supone que es html lo que desea usar. Si es solo una cadena, puede usar updateNoteP.innerText en su lugar
Si pasa pID como "noteContent1", esto cambiará el contenido de la 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 } }