im using Span text fields, and i want it to be cleared when clicking on them, how do i do?
My code looks like this:
<div class="text3">
<span contenteditable="true">något</span>
</div>
im using Span text fields, and i want it to be cleared when clicking on them, how do i do?
My code looks like this:
<div class="text3">
<span contenteditable="true">något</span>
</div>
How would you edit the text if you clear it everytime you click ? Anyway here is a trick :
<div class="text3">
<span contenteditable="true" onclick="this.innerText=''">något</span>
</div>
Use the node’s textContent
property to set the “inner” text, which is the actual text node.
To clear the text when the span gets the focus for editing its content, use the onfocus
attribute to set an empty string:
span{
display:inline-block;
min-width:30px;
min-height:18px;
outline:1px dotted grey;
}
<div class="text3">
<span contenteditable="true" onfocus="this.textContent='';">något</span>
</div>
Since the content will be set to empty string, the <span>
inline element will be nearly invisible without entering any text. You probably should add some visual to the interface in order to guide the user. The above CSS will keep it at arbitrary minimum dimensions, adding an outline to keep the “input” from disappearing.
Also, you could select the text content instead of removing it. Unfortunately, it seems the Range interface doesn’t work for contenteditable non-input elements, so that’d be qualified for another question.