An input element on a web page is defined like the following to prevent passwords being pasted in:
<input type="password" class="field" id="password" name="password" title="Enter your password" maxlength="50" onpaste="javascript:return false;" size="38" tabindex="2">
I want to enable pasting in this input field. Using Firebug or equivalent I can edit the onpaste function so the code is:
onpaste="javascript:return true;"
This is fine for me, but for other users, I want a simpler solution. What came to mind is that if I could give them some Javascript, they could open the console and paste in a simple one-liner. However when I tried to do this the Javascript had no effect. I tried the following and neither worked.
$('#password').onpaste=""
$('#password').onpaste="javascript:return true;"
What am I doing wrong?
An input element on a web page is defined like the following to prevent passwords being pasted in:
<input type="password" class="field" id="password" name="password" title="Enter your password" maxlength="50" onpaste="javascript:return false;" size="38" tabindex="2">
I want to enable pasting in this input field. Using Firebug or equivalent I can edit the onpaste function so the code is:
onpaste="javascript:return true;"
This is fine for me, but for other users, I want a simpler solution. What came to mind is that if I could give them some Javascript, they could open the console and paste in a simple one-liner. However when I tried to do this the Javascript had no effect. I tried the following and neither worked.
$('#password').onpaste=""
$('#password').onpaste="javascript:return true;"
What am I doing wrong?
$
is jQuery; please correct the tags if that is not correct.
– apsillers
Commented
Jun 19, 2015 at 13:10
The problem is that you're trying to bine jQuery with regular JavaScript. You can just use pure JavaScript to remove the onpaste listener.
document.getElementById('password').onpaste=""
If you want to use jQuery instead, you can use:
$('#password')[0].onpaste=""
which would give the same result.
Just remove the onpaste
attribute of the DOM element, not of the jQuery object which does not have a onpaste
property. Your code is creating a property (set to a string) that is just being ignored.
In the example below, you can click the button and paste will be re-enabled.
document.querySelector('button').addEventListener('click', function() {
document.getElementById('password').removeAttribute('onpaste');
})
<input type="password" class="field" id="password" name="password" title="Enter your password" maxlength="50" onpaste="javascript:return false;" size="38" tabindex="2">
<button>Allow pasting</button>
$('#password').onpaste=""
$('#password').onpaste="javascript:return true;"
Are not valid functions. For firing event which es from paste, you must write function like this
$('#password').bind("paste", function(e) {
callback();
});
But solution for your question is here http://jsfiddle/9et20v36/, in click
function from button allow
, I remove onclick
attribute from input which allows to paste text.
So for pure javascript use
document.getElementById('password').removeAttribute('onpaste');
or jQuery way
$('#password').prop('onpaste', null);