Skip to content

Instantly share code, notes, and snippets.

@drouillard
Created March 9, 2011 21:03
Show Gist options
  • Save drouillard/863002 to your computer and use it in GitHub Desktop.
Save drouillard/863002 to your computer and use it in GitHub Desktop.
Inserting Link in JavaScript around a selection
Fredrick Ring - http://groups.google.com/group/jquery-en/browse_thread/thread/6902e3e091ec9e4b/f8a135f66d533265?pli=1
Ok, I did it myself (+ the web). In case anyone is interested:
function wrapAsLink(url){
var textarea = document.getElementById("myTa");
var len = textarea.value.length;
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var sel = textarea.value.substring(start, end);
var replace = '<a href="'+url+'">' + sel + '</a>';
textarea.value = textarea.value.substring(0,start) + replace +
textarea.value.substring(end,len);
}
On Feb 22, 7:11 pm, Frederik Ring <frederik.r...@gmail.com> wrote:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment