JQuery nos presenta la funcion .selected() para esto, pero solo sirve con inputs del tipo "text" o con "textarea".
Aquí veremos como conseguir el texto seleccionado de un DIV ó el elemento que deseémos.
Código:
/* attempt to find a text selection */Fuente: www.elwebmaster.com
3 function getSelected() {4 if(window.getSelection) { return window.getSelection(); }5 else if(document.getSelection) { return document.getSelection(); }6 else {7 var selection = document.selection && document.selection.createRange();8 if(selection.text) { return selection.text; }9 return false;10 }11 return false;12 }13 /* create sniffer */14 $(document).ready(function() {15 $('#content-area').mouseup(function() {16 var selection = getSelected();17 if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {18 alert('Sending the following text to the server via AJAX: ' + selection);19 $.ajax({20 type: 'post',21 url: 'ajax-selection-copy.php',22 data: 'selection=' + encodeURI(selection)23 });24 }25 });26 });
No hay comentarios. :
Publicar un comentario
Sentite libre de comentar, criticar y/o aportar tu granito en este (proyecto de..) mar de conocimiento ;)