javascript - How do I make a link using keybindins, and selected text -
i looking way ctrl-l, popups box to insert link, on selected text. stackexchange editor, i'm using right now!
the links preferably valid html
, , if can choose both markdown
, valid html
, awesome!
i not find anywhere on so, or interwebs.
what have done before:
$("#mark").click(function () { var range = window.getselection().getrangeat(0); var newnode = document.createelement('mark'); range.surroundcontents(newnode); return false; });
button:
<a href="javascript:void(0)" id="mark">mark</a>
when clicking mark
button apply html mark
tag
<mark>selected text</mark>
i want same feaute stackexchange editor:ctrl-lget box:
edit
my new code :
while playing promptbox using javascript[1], managed way want it.
<button onclick="link()">link</button> <p id="demo"></p> <script> function link() { var link = prompt("insert hyperlink", "http://"); var title = prompt("insert hyperlink title", "title"); var name = prompt("insert hyperlink name", "name"); if (link != null) { document.getelementbyid("demo").innerhtml = "\<a href=\"" + link + "\" title=\" " + title + " \"> " + name + " </a>"; } } </script>
trying work:
to content of html
title
tag, when inserting link.example:
<a href="https://www.example.nl/example" title="*link*: *title*">*title*</a>
reference:
using jquery, can use on keydown
trigger capture keys pressed, :
$('#your_textarea').on('keydown', function(e){ //check if ctrl-l if(e.keycode == 76 && e.ctrlkey) { var link = prompt("enter link : "); } });
now, link user entered in link
variable. can want (ex: append editor's textarea).
note : have use keydown
event, keypressed
and keyup
events not work this.
here's fiddle : http://jsfiddle.net/js1p5h9n/
Comments
Post a Comment