3

I have an image. Here,I have written jQuery that looks like below :

jQuery('.image').click(function(){
    var path = $(this).attr('src');
});

Now, I want to fire copy event for this path.When I press ctrl+v button then it print above path so what jQuery should I have to write?

I need to fire copy event automatically. So it is possible?

Nisarg Bhavsar
  • 946
  • 2
  • 18
  • 40

2 Answers2

1

Link

html

<img class="image" src="http://pic.1fotonin.com/data/wallpapers/93/WDF_1369581.jpg" />  
<img class="image" src="https://c7.staticflickr.com/3/2538/3742771246_2648fa4e6e_b.jpg" />

css

.image{
width:200px;
}

javascript

$('.image').click(function(){
// Create an auxiliary hidden input
 var aux = document.createElement("input");

// Get the text from the element passed into the input
aux.setAttribute("value", $(this).attr('src'));

// Append the aux input to the body
document.body.appendChild(aux);

// Highlight the content
aux.select();

// Execute the copy command
document.execCommand("copy");

// Remove the input from the body
document.body.removeChild(aux);
 alert($(this).attr('src')); 
});
Zubair sadiq
  • 500
  • 6
  • 23
0

I think you want this

<script>
$(document).ready(function(){
    var path;
    $(document).on('click','.image',function()
    {
      path = $(this).attr('src');
    });

    $(document).on('keydown',function(evt)
    {
       if(evt.ctrlKey && evt.which == 86)
       {
       $('#field').html(path);
       }
   });
});
</script>

<body>
   <img src="sample/welcome/thing.jpg" alt="no image" class="image"/>

    <p id="field"></p>
 </body>
Vijay Wilson
  • 516
  • 7
  • 21