4

I am developing a Hybrid application using IBM Worklight, HTML5, CSS3 and javascript. Is there any possible way to disable text selection on input fields?

I have a username and password field on the screen and want to disable the user from selecting text within the input fields. Also, copy paste should be disabled.

Thanks

AAV
  • 61
  • 1
  • 5
  • Nope, I don't think there is? The usual ways of preventing text selection doesn't really work inside elements where text selection is considered a crucial part of the functionality. – adeneo Mar 31 '14 at 22:05
  • `element.onmousedown = function(){ return false; };` maybe? (Obviously other events need to specifically handled also, if so.) – David Thomas Mar 31 '14 at 22:32
  • Can you confirm that you still want the user to still be able to edit the contents of the input boxes? – Chuck Le Butt Mar 09 '16 at 12:54

3 Answers3

1

you can use below code to disable copy and paste

$('input,textarea').bind('cut copy paste', function (e) {
    e.preventDefault(); //disable cut,copy,paste
});
Iqbal Pasha
  • 1,318
  • 1
  • 8
  • 12
0

To disable pasting text into input field,

$('#myInput').bind('input', function(e) {
    $("#myInput").val("");    //This will reset input value to empty
});
Munawir
  • 3,346
  • 9
  • 33
  • 51
-2

There is an answer already on SO for selecting that will probably give you what you need: Prevent element from taking part in text selection

There are also answers on SO for disabling pasting on some browsers: Disable pasting text into HTML form

Keep in mind though that the above will not work in every browser.

Community
  • 1
  • 1
Neil Cresswell
  • 1,145
  • 8
  • 20