I trying to copy to clipboard the values inside the input fields. The code to get these values is working fine, when I click on the button for "copy", with the alert() function as little debug, I can see the right value inside... but when I try to take this value to copy to clipboard, an error comes up. I have seen a lot of example around the web, but only for an element... So, this is my code that shows the right value, but not works for a copy.
$(document).ready(function() {
$('div#screen_wrap').each(function() {
var elem = $(this);
$("#copycoords", elem).click(function() {
var coords = $("#coords", elem).val();
coords.select(); // This line is the error
document.execCommand("copy", false);
alert(coords);
});
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div id="screen_wrap" class="glass">
<div><img src="img.jpg" /></div>
<p>Country 1</p>
<input type="text" value="47.529016,19.050963" id="coords" />
<input type="button" value="Copia" id="copycoords" class="btn1" />
</div>
<div id="screen_wrap" class="glass">
<div><img src="img.jpg" /></div>
<p>Country 2</p>
<input type="text" value="41.662055,-0.894292" id="coords" />
<input type="button" value="Copia" id="copycoords" class="btn1" />
</div>
<div id="screen_wrap" class="glass">
<div><img src="img.jpg" /></div>
<p>Country 3</p>
<input type="text" value="38.461808,27.217074" id="coords" />
<input type="button" value="Copia" id="copycoords" class="btn1" />