zeroclipboard / zeroclipboard-rails

ZeroClipboard rails gem
http://zeroclipboard.org/
MIT License
125 stars 36 forks source link

Muultiple elements on same page in rails? #36

Open msumeet opened 8 years ago

msumeet commented 8 years ago

I have 10 elements on one page in each do loop that should allow user to copy when someone clicks on button. I managed to add copy to clipboard functionality to rails using Zeroclipboard-rails gem. As I have multiple items on same page I have changed Id to Class.

$(document).ready(function() {
    var clip = new ZeroClipboard($("#d_clip_button"))
  });
$(document).ready(function() {
    var clip = new ZeroClipboard($(".class_name"))
  });

But whenever I click on copy to clipboard button on any of 10 elements It selects the value of first item. How can I Fix This?

Here is the complete code. Thanks.

  <button class='my_clip_button' data-clipboard-target='fe_text' data-clipboard-text='Default clipboard     text from attribute' id='d_clip_button' title='Click me to copy to clipboard.'>
<script>
  $(document).ready(function() {
    var clip = new ZeroClipboard($(".my_clip_button"));

  $("#d-clip_button").on("click", function(){
    $("#fe_text").val("Copy me!");
  });
</script>
JamesMGreene commented 8 years ago

The code you've shown doesn't really correlate with your description of the scenario. For example, you said you have 10 clipped buttons but you've only shown the HTML for 1 of them.

Also, what is the intent of your "click" handler? It's very unclear to me.

Last but not least, this wouldn't be anything specific to the "zeroclipboard-rails" Gem, so ideally you should raise this question on the core ZeroClipboard repo instead.