2
<html>
<script>
$('#pasteButton').click( function() {

  // place code for this button
  // ctrl + v shortcut should work for this button
  // how?
  }

});

</script>
<body>
    <input type="button" id="pasteButton" value="Paste" />
</body>
</html>

Thanks in advance. Similarly, I also need to cut / copy selected text from a textarea using buttons.

update from comment section : I have a text area where I select some text and if I click on pasteButton that selected text need to be pasted in my text area

2
  • may be you should try Artificial Intelligence. Commented Apr 9, 2013 at 10:55
  • First of all remove extra } from click function. Commented Apr 9, 2013 at 10:56

3 Answers 3

7

First you have to listen to the keydown event. If it is control+v then you can trigger your function You can use the following code for that purpose:

HTML

<input type="button" name="pasteButton" value="press me" id="pasteButton"/>

JAVASCRIPT

  $(window).keydown(function(event) {
          if(event.ctrlKey && (event.which == 86 || event.which==118)) { 
              $('#pasteButton').trigger("click")
            event.preventDefault(); 
          }
        });
        $('#pasteButton').click( function(){
            alert("hello");
        });

SAMPLE JSFIDDLE

Sign up to request clarification or add additional context in comments.

8 Comments

am sorry i think my question is not clear. i want the same functionality of 'ctrl+v' on click of 'pasteButton'
@mbsr, If like that, what is your real requirement? What are you trying to achieve?
i have a text area where i select some text and if i click on pasteButton that selected text need to be pasted in my text area.
@mbsr, sorry for the late reply, so you need only copy functionality? Or you need 'ctrl+v' short cut too?
@mbsr, its little more complicated than you expect, refer these similar SO question stackoverflow.com/questions/717224/… stackoverflow.com/questions/275761/…
|
1

Taken from this post:

How to detect Ctrl+V, Ctrl+C using JavaScript?

$(document).ready(function()
{

    $('#pasteButton').click( function()
    {
        // Your click functionality here
    }

    var ctrlDown = false;
    var ctrlKey = 17, vKey = 86;

    $(document).keydown(function(e)
    {
        if (e.keyCode == ctrlKey) ctrlDown = true;
    }).keyup(function(e)
    {
        if (e.keyCode == ctrlKey) ctrlDown = false;
    });

    $(document).keydown(function(e)
    {
        if (ctrlDown && e.keyCode == vKey) {
           // ctrl + V clicked, click the paste button.

           $('#pasteButton').click();
           return false;
        };
    });
});

Comments

0

Just use accesskey attribute of html tag for more detail see following link

http://www.w3schools.com/tags/att_global_accesskey.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.