8

So I want the features of a contenteditable div (text selection via keyboard being the main one), but I don't want to allow the user to edit the text - I presumed a readonly="readonly" would work, but sadly it doesn't.

Do we know if there's a way to do it? The contenteditable div may have nested tags inside (<p>'s, <h1>'s).

I want to avoid a textarea (that's what this is for), as it doesn't allow me to do other things I need to be able to do.

Is there a nice way to do this without javascript? And if there isn't, is there a small snippet of javascript that will still allow copying, highlighting etc?

Thanks.

Here's an example: http://codepen.io/anon/pen/dxeAE

Matthew Evans
  • 245
  • 4
  • 13
  • please show us your code –  Oct 14 '14 at 12:13
  • 2
    What is stopping your end user selecting text in a div *without* `contenteditable`? I can use the keyboard for text selection in Chrome... – CodingIntrigue Oct 14 '14 at 12:17
  • http://codepen.io/anon/pen/dxeAE here you go. And you cannot select text the way you can in a textarea/contenteditable - I need that functionality without the option to edit :) The key thing here is selecting text with a keyboard, not a mouse - that can obviously be done regardless :) – Matthew Evans Oct 14 '14 at 12:19

4 Answers4

5

I made a jquery solution/workaround. What is does is prevent the default action when a key is pressed:

$(document).ready(function(){
  $("#editor").on("keypress", function(e) {
      e.preventDefault();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="editor" contenteditable="">
   <h1>Heading</h1>
   <p>Test</p>
</div>
Andreas Furster
  • 1,558
  • 1
  • 12
  • 28
1

You could also try this, plain javascript solution

document.getElementById('div').onkeydown = function (e) {
    var event = window.event ? window.event : e;
    return !(!~[37, 38, 39, 40].indexOf(e.keyCode) && !e.ctrlKey);
}

This allows selection using arrow keys and copying the same.

Salman
  • 9,299
  • 6
  • 40
  • 73
  • 1
    Yeah that's what scares me - keyCodes can sometimes be not what you expect – Matthew Evans Oct 14 '14 at 12:38
  • Oops.. btw, @AndreasFurster your solution doesn't work in Mozilla (latest) for me – Salman Oct 14 '14 at 12:39
  • Then I could only think of storing initial html in `h` and `return ((this.innerHTML != h) && (this.innerHTML = h))` in an `onkeyup`. That's a shame. :P – Salman Oct 14 '14 at 12:45
  • I see... Or think about how important it is to select with te keybord on all browsers... You can still select with the cursor in FF... – Andreas Furster Oct 14 '14 at 12:49
0

You can probably add an asp:Panel in the div and make the panel.Enabled = false Controls inside panel will not be accessible.

jrbedard
  • 3,662
  • 5
  • 30
  • 34
0

To make all controls within a div locked or unlocked, try this :

<div id="lockableDiv">
....
</div>

lockDiv = function(lockIt){
    if(lockIt){    
        $('#lockableDiv :input').attr('readonly', true);
    }else{
        $('#lockableDiv :input').removeAttr('readonly');
    }
}
  • Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". – abarisone Sep 19 '16 at 13:58