SELECCIONAR PALABRA CON CLICK Y PASARLA A INPUT
El script selecciona cualquier palabra del text box (donde copiamos texto) con un click y lo pasa a la variable t que lo pasa a val del input con id=resultado
						
<head>
<script src="../scripts/jquery-3.6.4.min.js"></script>
	<script>
		$(document).ready(function(){
			$("#texto").click(function() {
				var t = '';
				if (window.getSelection && (sel = window.getSelection()).modify) {// Webkit, Gecko
					var s = window.getSelection();
					if (s.isCollapsed) {
						s.modify('move', 'forward', 'character');
						s.modify('move', 'backward', 'word');
						s.modify('extend', 'forward', 'word');
						t = s.toString();
						s.modify('move', 'forward', 'character'); //clear selection
					}else {
						t = s.toString();
					}
				} else if ((sel = document.selection) && sel.type != "Control") { // IE 4+
					var textRange = sel.createRange();
					if (!textRange.text) {
						textRange.expand("word");
					} // quita los "trailing spaces"
					while (/\s$/.test(textRange.text)) {
						textRange.moveEnd("character", -1);
					}
				t = textRange.text;
				}
				if(t.length!=0){
					$("#resultado").val(t);
				}
			});
		});
	</script>
	<style> 
		.bloque1{
			width:200px;
			margin: 0 auto;
			margin-top:20px;
			padding:20px;
			background-color:#CFC;  
		}
		.bloque2{
			width:240px;
			margin: 0 auto;
			margin-top:20px;
		}
	</style>
</head>
<body>
<div class="bloque1" >
			Copie el texto para controlar que haciendo click sobre una palabra se transcribe 
			en el input de abajo. Asimismo si se escribe un texto ocurrirá lo mismo al clicar una palabra.
	</div>  
	<div class="bloque2">
		<textarea  id="texto" name="poner" style="width:240px; height:200px; "></textarea>
	</div>
	<div  class="bloque2">
		<input id="resultado" type="text" value="" style="width: 240px;"/>
	</div>	
</body>
							
					
© IQSystems 2023