Question:
Good morning folks, how can I delete the text "This I must delete" without deleting the div with id "noDelete" with Jquery?
PS: it doesn't help me to put it inside an HTML tag and then call it from jquery.
Thanks in advance.
#noBorrar{
border: 1px solid blue;
height: 50px;
width: 100px;
margin-left: 5px;
}
#contenedor{
border: 1px solid green;
height: 100px;
}
<div id="contenedor">
Esto debo eliminar
<div id="noBorrar">
No eliminar esto
</div>
</div>
Answer:
Selecting the Jquery array gives you the javascript element, this allows taking the outerHTML, and assigning it after cleaning.
let noBorrar = $("#noBorrar")[0].outerHTML
$("#contenedor").html("");
$("#contenedor").html(noBorrar);
#noBorrar{
border: 1px solid blue;
height: 50px;
width: 100px;
margin-left: 5px;
}
#contenedor{
border: 1px solid green;
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="contenedor">
Esto debo eliminar
<div id="noBorrar">
No eliminar esto
</div>
</div>