javascript - JS to populate Bootstrap modal with selected text only seems to work once -
i'm using boostrap create modal dialog allows users enter comments on given page. wanted expand allow me capture selected text , add content field in modal:
$( document ).on("shown.bs.modal", function(){ var selectedtext = ''; var selectedtext = window.getselection().tostring(); if(selectedtext != "") { $('#comment_content').val("> " + selectedtext); } });
#comment_content
id of input field modal form.
this works great first time modal shown, fails work on subsequent uses.
whenever click comment button selected text:
%a.btn.btn-primary.btn-sm{:id => 'comments_toggle', "data-toggle" => "modal", "data-target" => "#commentmodal"} post comment
i modal back, selection isn't applied #comment_content
field.
the other interesting thing, perhaps, won't work on page reload, either. until tab page destroyed, or until go page, every click of button text selected gives me empty #comment_content
field.
try embed click function button:
$( document ).on("shown.bs.modal", function(){ $('#comments_toggle').click(function(){ var selectedtext = ''; var selectedtext = window.getselection().tostring(); if(selectedtext != "") { $('#comment_content').val("> " + selectedtext); } }); });
Comments
Post a Comment