Skip to content

Instantly share code, notes, and snippets.

@alanerzhao
Created November 15, 2013 03:21
Show Gist options
  • Save alanerzhao/7478567 to your computer and use it in GitHub Desktop.
Save alanerzhao/7478567 to your computer and use it in GitHub Desktop.
var text3 = document.getElementById("text3");
var text2 = document.getElementById("text2");
text2.focus()
text2.onfocus = function () {
this.select();
}
var tips = document.createElement("span");
text3.onselect = function () {
//console.log(this.selectionStart,this.selectionEnd)
//console.log(this.value.substring(this.selectionStart,this.selectionEnd));
// console.log(textSelect(this));
tips.className="tips"
tips.innerHTML = textSelect(this);
this.parentNode.insertBefore(tips,null);
}
text3.onclick = function () {
this.setSelectionRange (0,5);
console.log(this.setSelectionRange(0,5))
}
function selectText (obj,startIdx,stopIdx) {
if(obj.setSelectionRange) {
obj.setSelectionRange(startIdx,stopIdx);
} else if (obj.createTextRange) {
var range = obj.createTextRange();
range.collapse(true);
range.moveStart("character",startIdx);
range.moveEnd("character",stopIdx - startIdx);
range.select();
obj.focus();
}
}
function textSelect (obj) {
if(typeof obj.selectionStart == "number") {
return obj.value.substring(obj.selectionStart,obj.selectionEnd);
} else if(document.selection) {
return document.selection.createRange().text
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment