calling a php script using $.POST in javascript -
i have html form textarea
, button
calls function below when part of text selected. de id of clicked button , selected text want call php script post
method.
$(document).ready(function() { $(".postbit_buttons").on("click", ".quoted_clicked", function() { var post = this.id; var selected = getselectedtext(); alert(" post = " + post + " seleted text = " + selected); $.post("test_quoted.php", { pid: post, selection: selected }) .done(function() { alert("done") }); }); });
function getselectedtext()
selection found here. first alert shown correct information. however, after clicking ok following error message on browser console:
typeerror: 'collapse' called on object not implement interface selection.
i have used similar construct in part of forum software of part, , works.
i have pain in eyes of staring @ this, cannot find cause. there can me on this?
@rory code of function getselectedtext() is:
function getselectedtext() { var selectedtext=( window.getselection ? window.getselection() : document.getselection ? document.getselection() : document.selection.createrange().text ); if(!selectedtext || selectedtext=="") { if(document.activeelement.selectionstart) { selectedtext = document.activeelement.value.substring( document.activeelement.selectionstart . document.activeelement.selectionend); } } return selectedtext; }
thanks rory's question tried things out. discovered variable "selected" in selected text stored cause of error. use following version of getselectedtext():
function getselectedtext () { if (window.getselection) { // browsers, except ie before version 9 var range = window.getselection (); return range.tostring (); } else { if (document.selection.createrange) { // internet explorer var range = document.selection.createrange (); return range.text; } } }
and works!! sorry bothering you. practice programming since 1968, 100% scientific computing fortran. javascript new me.
regards, ad bakker
Comments
Post a Comment