I've got contenteditable div as below.
<div style=" border:solid 1px #D31444" contenteditable="true">12 some text...</div>
What I need is, when I click on the div, all the text will automatically get selected. Can you give me solution please?
I've got contenteditable div as below.
<div style=" border:solid 1px #D31444" contenteditable="true">12 some text...</div>
What I need is, when I click on the div, all the text will automatically get selected. Can you give me solution please?
Try this:
<div style="border:solid 1px #D31444"
contenteditable="true"
onclick="document.execCommand('selectAll',false,null)">12 some text...</div>
Update: Note that document.execCommand
is now deprecated although it still works.
focus
event, because using the click event will highlight everything whenever the user tries to click to position the caret. –
Chlorate focus
does not work. Binding to the click
event did select the text: $('.editable').on('click', function () { document.execCommand('selectAll', false, null); });
–
Norris focus
event does work, but there are subsequent events (positioning the cursor, maybe) that undo the selection. I tried stopPropagation()
, but that didn't work for me. –
Norris selectAll
on focus
correctly, and I filed an issue about it. click
works, but as Tim pointed out, usability suffers when the user wants to select only part of the text. –
Proteiform focus
a div, unless you set its tabindex
property. –
Saccharase window.getSelection().selectAllChildren(div);
–
Darell This will do it. The timer is there for Chrome and Safari because in those browsers, the native browser behaviour that selects the whole element seems to trigger after the focus event, thereby overriding the effect of the selection code unless postponed until after the focus event:
var div = document.getElementById("editable");
div.onfocus = function() {
window.setTimeout(function() {
var sel, range;
if (window.getSelection && document.createRange) {
range = document.createRange();
range.selectNodeContents(div);
sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(div);
range.select();
}
}, 1);
};
preventDefault()
is out. stopPropagation()
only stops the event from propagating to the next target, which is not useful for this. –
Chlorate window.getSelection()
and document.createRange()
, which are both now standardized, are supported in most browsers but not IE < 9. IE does have its own selection/range API though, accessed here via document.body.createTextRange()
. –
Chlorate Element.prototype.SelectAll=function(d){d=this;var sel,range;window.getSelection&&document.createRange?(range=document.createRange(),range.selectNodeContents(d),sel=window.getSelection(),sel.removeAllRanges(),sel.addRange(range)):document.body.createTextRange&&(range=document.body.createTextRange(),range.moveToElementText(d),range.select())}
–
Nancynandor window.getSelection().selectAllChildren(div);
–
Darell selectAllChildren
existed in 2010 but not all browsers at the time followed the spec precisely, so I didn't use it in this answer. I don't know whether all current browsers follow the spec now, tbh. –
Chlorate selectAllChildren
existed in all browsers in 2010 but was not implemented the same in all of them, so it's not as simple as just saying "MDN lists support for all browsers" and leaving it at that. However, I will check and update if necessary when I get a moment. –
Chlorate Try this:
<div style="border:solid 1px #D31444"
contenteditable="true"
onclick="document.execCommand('selectAll',false,null)">12 some text...</div>
Update: Note that document.execCommand
is now deprecated although it still works.
focus
event, because using the click event will highlight everything whenever the user tries to click to position the caret. –
Chlorate focus
does not work. Binding to the click
event did select the text: $('.editable').on('click', function () { document.execCommand('selectAll', false, null); });
–
Norris focus
event does work, but there are subsequent events (positioning the cursor, maybe) that undo the selection. I tried stopPropagation()
, but that didn't work for me. –
Norris selectAll
on focus
correctly, and I filed an issue about it. click
works, but as Tim pointed out, usability suffers when the user wants to select only part of the text. –
Proteiform focus
a div, unless you set its tabindex
property. –
Saccharase window.getSelection().selectAllChildren(div);
–
Darell The problem with focus
event on a div
is that it can't fire because it thinks a div
should not be editable. Editable contents in the DOM are marked with tabindex
in the background, so in order for your div to receive the onfocus
event, you need to explicitly declare the div
's tabindex
property.
HTML:
<div style=" border:solid 1px #D31444" contenteditable="true" tabindex="1" onfocus="document.execCommand('selectAll',false,null)" >12 some text...</div>
That should work with onfocus
.
setTimeout
for 150
ms so it won't select the whole document. Hope this helps you all! –
Nymphet <style>
& <script>
sections (or .css
& .js
pages), like the jsGods intended and it won't be an issue; you shouldn't need tabindex
either. You could use something like a plain ol' <div id='myDiv'> </div>
styled like #myDiv{border:solid 1px #d14;}
or whatever, and js myDiv.addEventListener('click', function(e){this.contentEditable = true; this.focus(); document.execCommand('selectAll', false, null);});
. (Works on FF anyway) –
Popeyed © 2022 - 2024 — McMap. All rights reserved.
SelectAll()
. Add it to the div withonclick
andonfocus
, includingtabindex="0"
and someid
for easy selection. The function:const editableDiv = document.getElementById("that-div");
window.getSelection().selectAllChildren(editableDiv);
– Darell