What will be the approach to selecting text in the span tag when considering browser compatibility? Example:
jQuery().html('<p>Hello world <span>lorem ipsum</span> my good friend!');
I want the part to lorem ipsumbe selected with the cursor.
I have this code that selects text:
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);
}
}
source
share