I want to select the div content on click button.
HTML
<div id="divid">Hello This div content have to be select. </div>
<button onclick="selectText(divid);"> Select Div</button>
JS
function selectText(divid) {
if (document.selection) {
var div = document.body.createTextRange();
div.moveToElementText(document.getElementById("divid"));
div.select();
}
else {
var div = document.createRange();
div.setStartBefore(document.getElementById("divid"));
div.setEndAfter(document.getElementById("divid"));
window.getSelection().addRange(div);
}
}
https://jsfiddle.net/rajagopalx/xds4y0en/
Try the below @Rajagopal Subramanian
function selectText(containerid) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));
window.getSelection().addRange(range);
}
}
<div id="divid">Hello This div content have to be select.</div>
<button onclick="selectText('divid')">Select</button>
function selectText(containerid) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));
window.getSelection().addRange(range);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With