renisenbb
asked on
Javascript: Range object
I see different behavior in IE-11 (text becomes blue) and FF (text becomes grey) with this code.
In IE, the text can be copied to the clipboard because it's selected, and in FF it cannot.
<p id="p1">
This is a line of text
</p>
element = document.getElementById("p 1");
var selection = window.getSelection();
txtRange = document.createRange();
txtRange.selectNodeContent s(element) ;
selection.removeAllRanges( );
selection.addRange(txtRang e);
In IE, the text can be copied to the clipboard because it's selected, and in FF it cannot.
<p id="p1">
This is a line of text
</p>
element = document.getElementById("p
var selection = window.getSelection();
txtRange = document.createRange();
txtRange.selectNodeContent
selection.removeAllRanges(
selection.addRange(txtRang
ASKER
i still see grey text in your jsfiddle
but I see blue :)
Chrome
Version 58.0.3029.81 (64-bit)
Chrome
Version 58.0.3029.81 (64-bit)
ASKER
I was looking at firefox. Yes, i see that it works for Chrome. Thanks for that.
Is there a solution for Firefox too?
Is there a solution for Firefox too?
ummm...
add this
add this
Document.getSelection();
ASKER
It's not working in Firefox. I have this:
element = document.getElementById("p 1");
var selection = window.getSelection();
txtRange = document.createRange();
txtRange.selectNodeContent s(element) ;
selection.removeAllRanges( );
selection.addRange(txtRang e);
//window.focus();
Document.getSelection();
window.focus();
element = document.getElementById("p
var selection = window.getSelection();
txtRange = document.createRange();
txtRange.selectNodeContent
selection.removeAllRanges(
selection.addRange(txtRang
//window.focus();
Document.getSelection();
window.focus();
did you try
window.focus();
Document.getSelection();
window.focus();
Document.getSelection();
ASKER
Yes, that didn't work either. You mean document.getSelection() correct?
try adding blur/focus
window.blur();
window.focus();
ASKER
It seems to work in Firefox if i add the attribute contenteditable=true to the <p> element. IE and Chrome continued to work with this addition.
Not sure what the side effects of doing this will be....
Not sure what the side effects of doing this will be....
click and you can edit it :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much, it was a very tricky issue for me
meantime, here is the solution, using jQuery
call this and pass id of any element...
call this and pass id of any element...
function SelectText(element) {
var doc = document,
text = doc.getElementById(element),
range, selection;
if (doc.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
}
add
Open in new window