admin管理员组文章数量:1414621
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?
Share Improve this question edited Jun 19, 2015 at 13:09 apsillers 116k18 gold badges248 silver badges247 bronze badges asked Jun 19, 2015 at 13:05 PhilPhil 1,9274 gold badges26 silver badges49 bronze badges 5- $('#password').on('paste', function(e){ return true; }); – loli Commented Jun 19, 2015 at 13:08
-
I assume your
$
is jQuery; please correct the tags if that is not correct. – apsillers Commented Jun 19, 2015 at 13:10 - 1 Am I missing something? Why can't you just not put the onpaste attribute there? – Anonymous Commented Jun 19, 2015 at 13:11
- 1 @Anonymous I thought the same thing, but the OP is dealing with an input that is currently set not to allow pasting, but they want to enable pasting again – Ruan Mendes Commented Jun 19, 2015 at 13:13
- @JuanMendes Ah, my bad, I thought they were talking about their own input element. That makes more sense. – Anonymous Commented Jun 19, 2015 at 13:15
3 Answers
Reset to default 4The 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);
本文标签: jqueryHow to replace the onpaste function in javascriptStack Overflow
版权声明:本文标题:jquery - How to replace the onpaste function in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745149574a2644857.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论