javascript - Get into the selected text div -
i have 1 mini question select text in selected div.
i have created demo codepen.io .
in demo can see there 1 blue button , text in textarea. want when select select text. , click blue button selected text should <div class="selected">select text.</div>
after clicking blue button.
how can that. can me in regard ?
$(document).ready(function() { $("body").on("click", ".bold", function() { // code goes here... }); });
html, body { width: 100%; height: 100%; padding: 0px; margin: 0px; font-family: helvetica, arial, sans-serif; -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; background-color: #fafafa; } .container { position: relative; width: 100%; max-width: 500px; margin: 0px auto; margin-top: 30px; } .editor { float: left; width: 100%; padding: 30px; box-sizing: border-box; -webkit-box-sizing: border-box; } .editbuttons { width: 100%; float: left; margin-bottom: 15px; } .bold { padding: 5px; border-radius: 3px; -webkit-border-radius: 3px; background-color: blue; position: relative; max-width: 150px; text-align: center; color: #ffffff; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="editbuttons"> <div class="bold">add in div</div> </div> <textarea class="editor" id="editor">select text. , click blue button. should add following div tag this: <div class="selected">select text.</div> </textarea> </div>
i guess you're looking this:
$(document).ready(function() { $("#btnselect").click(function() { var $area = $("textarea.editor"); var area = $area.get()[0]; var s = area.selectionstart; var e = area.selectionend; if (s == e) return; var text = $area.val(); var before = s > 0 ? text.substr(0, s) : ""; var selection = text.substr(s, e - s); var after = e < text.length - 1 ? text.substr(e) : ""; selection = "<div id=\"selected\">" + selection + "</div>"; $area.val(before + selection + after); return false; }); });
html, body { width: 100%; height: 100%; padding: 0px; margin: 0px; font-family: helvetica, arial, sans-serif; -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; background-color: #fafafa; } .container { position: relative; width: 100%; max-width: 500px; margin: 0px auto; margin-top: 30px; } .editor { float: left; width: 100%; padding: 30px; box-sizing: border-box; -webkit-box-sizing: border-box; } .editbuttons { width: 100%; float: left; margin-bottom: 15px; } .editbuttons { text-decoration: none } #btnselect { padding: 5px; border-radius: 3px; -webkit-border-radius: 3px; background-color: blue; position: relative; max-width: 150px; text-align: center; color: #ffffff; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <div class="editbuttons"> <a href="" id="btnselect">select</a> </div> <textarea class="editor" id="editor">select text. , click blue button. should add following div tag this: <div class="selected">select text.</div> </textarea> </div>
Comments
Post a Comment