aurelius2016 aurelius2016 - 3 months ago 13
HTML Question

Why does ZeroClipboard only copy first word of sentence?

in my rails app I'm trying to create a set of buttons that can be clicked to copy a sentence (the title of tips) which are stored in @all_tips. At the moment the copying is working but only for the first word (i.e. stops as soon as there's a space) and I'm not sure why and haven't been able to find any other guidance.

<% (@all_tips).each do |n| %>
<button class="btn btn-success copy_to_clipboard" data-clipboard-text= <%= n.title %> > <p> <%= n.title %></p> </button>
<% end %>


<script>
jQuery.fn.copyToClipBoard = function() {
var clip = new ZeroClipboard($(".copy_to_clipboard"));
}
$(function() {
$('.copy_to_clipboard').copyToClipBoard();
});
</script>


Any help would be greatly appreciated, thanks!

Answer

Add quotes around title like this:

data-clipboard-text="<%= n.title %>"

Without the quote, if the title is for example: My awesome title

The tag will look like this:

<button class="btn btn-success copy_to_clipboard" data-clipboard-text=My awesome title >

So the data-clipboard-text value is only "My", because the lack of quote consider the next space to be the end of the parameter value. With the quotes, the tag will look this:

<button class="btn btn-success copy_to_clipboard" data-clipboard-text="My awesome title" > 

data-clipboard-text value will be "My awesome title". Look at the color highlights in the previous code snippet, blue is the value and orange the parameter name.

Comments