Reputation: 21
I don't want to allow user to cut copy paste in textbox I have tried following code but it is not even bind to that element:
$(document).ready(function(){
$("userName").on("cut copy paste",function(e){
e.preventDefault();
});
});
Upvotes: 2
Views: 2853
Reputation: 3
So the code here looks mostly sane. Using an example from this blog post I made this jsFiddle and it definitely works, including on my Android device.
What I think is being overlooked here is two things:
IDs are meant to be unique to all elements on the page. One and only one element will be selected. The way to select this is: $('#id_name')
. An example of HTML with an ID:
<input name="field_name" id="id_name">
Classes are used by both CSS and selectors. They can be used on one or more elements. All elements with this class are selected. The way to select this is: $('.class_name')
. An example of HTML with a class:
<input name="field_name" class="class_name">
Names are generally used by forms to pass information back to the server once submitted; they are less used for selectors than IDs or classes. They can be used on one or more elements. All elements with this class are selected. The way to select this is: $('[name="field_name"]')
. An example of HTML with just a name:
<input name="field_name">
What I believe is actually going on is that the HTML for the field does not contain an ID and only a name. In addition, the selector as written is most likely incorrect. This selector would be designed to select a userName element Such as:
<userName foo="bar">This is a custom element</userName>
Most likely this is not what you want, and you most likely are missing the correct type of selector and perhaps the correct attributes associated to use this selector
Upvotes: 0
Reputation: 2424
Change the selector identity if it is id means use
$(document).ready(function(){
$("#userName").bind("cut copy paste",function(e){
e.preventDefault();
});
});
or if it is class
$(document).ready(function(){
$(".userName").bind("cut copy paste",function(e){
e.preventDefault();
});
});
Upvotes: 1
Reputation: 370
$(document).ready(function(){
$('#userName').bind("cut copy paste",function(e) {
e.preventDefault();
});
});
Upvotes: 2