<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body>div {padding:5px;}
</style>
</head>
<body>
<div>
<input type="text" value="casa"> <input type="button" class="copy" value="copiar al portapapeles">
</div>
<div>
<input type="text" value="avion"> <input type="button" class="copy" value="copiar al portapapeles">
</div>
<div>
<input type="text" value="perro"> <input type="button" class="copy" value="copiar al portapapeles">
</div>
</body>
</html>
<script>
document.querySelectorAll(".copy").forEach(el => el.addEventListener("click", copy));
function copy(e) {
// obtenemos el input
const input = this.previousElementSibling;
// Selecciona el contenido del campo
input.select();
// Copia el texto seleccionado
document.execCommand("copy");
}
</script>
Comentarios sobre la versión: Versión 2.0 - Seleccionando el contenido del input que se copia (0)
No hay comentarios