3

I'm attempting to make a span tag un-deletable inside a 'contenteditable' div:

<div contenteditable>Editable <span contenteditable="false">Read Only</span></div>

The read only span is indeed read only, but I can delete the whole span with a single delete press. Is there an attribute way to tell the span that it is not deletable?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Alon Weissfeld
  • 1,295
  • 6
  • 21
  • 35
  • From where are you deleting? Actually nothing sounds clear. – divy3993 May 21 '15 at 12:19
  • You are probably forced to this using js or jquery. Find all elems that are editable and store their non-editable text. After each edit you check if the non-edit text is still inside the control and if it is not you'll have to set it again. – Noel Widmer May 21 '15 at 12:21
  • @divy3993 When I say 'div contenteditable' It responds like a textarea. I've put a span in there so it's content is false meaning I can't edit it (i.e 'Read Only'). But, because the whole div is contenteditable I can edit/delete inside html. I'm trying to make this specific span untouched. – Alon Weissfeld May 21 '15 at 12:23
  • How are you making it behave like a textarea? Post that code because that is where youll have to implement this. – Brad C May 21 '15 at 12:40
  • 1
    @br4d.net this is all the html. I was just explaining the '
    – Alon Weissfeld May 21 '15 at 12:42
  • 1
    @br4d.net It's a feature that is supported by almost all browsers: http://caniuse.com/#feat=contenteditable – Martin Braun May 21 '15 at 12:56
  • I don't think this is possible without some javascript hackery. Even the best solution provided so far still lets me delete the span (but not edit it). You might be able to write a script that replaces it when it is deleted or intercepts key presses. – Brad C May 21 '15 at 13:05

4 Answers4

3

Make the div position: relative and the span position: absolute. This takes the span outside the normal document flow, still inside the div, but unable to be altered or removed:

div[contenteditable] {
  position: relative;
}
div[contenteditable] span {
  position: absolute;
  margin-left: 1em;
}
<div contenteditable>Editable <span contenteditable="false">Read only - Can't touch this </span></div>
misterManSam
  • 24,303
  • 11
  • 69
  • 89
3

So I figured out the best solution for me, very simple. A content editable div in not needed. Just a a few content editable spans.

            <div>
                <span contenteditable="true">
                    Editable text here.
                </span>
                <span contenteditable="false" style="font-weight: bold">
                    READ ONLY VALUES
                </span>
                <span contenteditable="true">
                    Edit more stuff here 
                </span>
             </div>
Alon Weissfeld
  • 1,295
  • 6
  • 21
  • 35
1

…still lets me delete the span (but not edit it).

You can't achieve this via the contenteditable attribute alone. It is working as intended. The contenteditable attribute does what it says: it allows someone to edit the content of the element.

Consider a jar with jam, inside a box which says you can change the content of the box. The jar has a label which says "you may not change the jam in this jar". This rule does not forbid you from throwing said jar into the trashcan because you never actually change the content of the jar by doing so.

I suggest, like Noel Widmer did, that you try to find another solution to your problem. If you can provide a high fidelity example we might suggest some alternatives.

Niklas Brunberg
  • 769
  • 7
  • 17
  • In the end, the goal is to make a widget that has some value that cannot be changed. It can be an input, textarea, or whatever. For a clearer example: If the untouched value is set to "Danny", I would set a placeholder "This kid is named Danny, He likes apples". This should be represented in a widget so I can edit everything except the value I passed as read only (Danny). I've tried doing it with jQuery but this get's very complicated to overcome all the cases of keypress types. Any suggestion would be great – Alon Weissfeld May 22 '15 at 00:22
  • I created a jQuery UI demo. You could of course do this without the entire UI library though. [See fiddle](https://jsfiddle.net/NiklasBr/fcv4afd5/). This demo does not allow nested elements but I hope it gives you an idea to work from. – Niklas Brunberg May 22 '15 at 10:12
0

are you using jquery. use this code on click span :

$(this).click(function(){

$('span').remove(); });

Mukesh Prajapat
  • 274
  • 2
  • 13