As you say @JuanSalvadorPortugal in your answer you could solve the problem with the property placeholder HTML if you change your element <span>
by an element <input>
, without However, I imagine that you are trying to emulate the behavior of this property directly in your <span>
element using javascript.
You could use the event blur
(which is just activated when the focus of the element in question is lost) by means of a listener that guarantees a continuous check of the element, then when the event is triggered you check if the element is empty (in your case a <span>
).
It would also be good to improve the CSS of the elements a bit
because if you are going to be changing the text that you have internally it would be good if the user could notice it, because when
a <span>
remains empty the width "is reduced" to 0 and for a user
common it would not be possible to notice that it is happening for sure. Of
every way I included css to your <span>
in my solution so that
appreciate the changes when making the onclick and the onblur.
Next I leave the code with what I have commented previously:
var span= document.getElementById("Texto_publicacion_user_main_click");
function publicar_usuario_texto() {
span.innerHTML ="";
}
span.onblur = function(){
if(span.innerHTML==""){
span.innerHTML ="Otro texto";
}
};
span {
width:30%;
display: inline-block;
background:#EEEEEE;
}
<span contenteditable="true" id="Texto_publicacion_user_main_click" name="hola" onclick="publicar_usuario_texto();">Publica algo aquí</span>
I hope it helps. Greetings