How do I copy and paste to a screen?

Asked

Viewed 86 times

0

Personal greetings,

I have a generic javascript file util.js with a Function not to allow copy and paste.

    $('input').bind('copy paste', function (e) {
   e.preventDefault();
});

On a specific page, I need a certain field to allow me to copy and paste and I didn’t want to get rid of the util.js that doesn’t allow it on the other pages of the application. Is there any way to allow pasting? Or turn off this validation for the specific page?

Thank you

Edit1: I called the unbind(); direct method on the page that way and it worked

    $(document).ready(function() {

    $("input").unbind();

However, it works for all screen inputs. I tried to pass the id/field name as parameter but it didn’t work. Ideally, only one field allows copying and pasting.

Thanks in advance

Edit2:

The field you need to allow copy and paste is this:

<td width="306" align="center">
                                <s:textfield id="idDesc%{#rowStatus.index}" cssClass="bg_input txtTable" cssStyle="%{listDespRec[#rowStatus.index].listError[2].aplicarCss}" name="listDespRec[%{#rowStatus.index}].observacaoLancamento" value="%{listDespRec[#rowStatus.index].observacaoLancamento}" size="35"  maxlength="30"/>
</td>

Any suggestions on how to pass this field in the unbind?

That way here it’s not working

$(document).ready(function() {

    $('#listDespRec[%{#rowStatus.index}].observacaoLancamento').unbind('copy paste');
  • If you put the above code on the page you want to copy and paste without preventDefault, it doesn’t work?

  • Opa @Leandrade, thanks for answering. It does not work, I believe that the first validation prevails over the page validation

  • Yes, this is because the validator system has not been removed. You could share the useful.js also to facilitate the response ?

  • Opa @Marcosjunior, thank you for the reply. The util.js file contains only this copy and paste validation that is called on the startup of all pages. As I edited in the question, passing the unbind(); right on the page I need to change, it allows copying and pasting. The question now is the field, as all inputs are disabling bind('copy Paste'). Passing the id/field name, it didn’t work. The ideal would be to disable only for the field

2 answers

1


Solution make the unbind to for specific input.

Example:

$( "#foo" ).bind( "click", handler );
$( "#foo" ).unbind( "click", handler );

Practical example

$('input').bind('copy paste', function(e) {
  e.preventDefault();
});

$('#permitecopiar').unbind('copy paste');
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<form >
  Não permite colar: <input type="text"><br> Permite colar: <input id="permitecopiar" type="text"><br>
</form>

  • Thanks for the answer. I added the field to the question by passing the id the way you suggested it didn’t work

  • Most likely you are not using the appropriate id, you can confirm on the Chrome console $('#o_seu_id') if it is correct

  • <input type="text" name="listDespRec[4]. observacLanking" size="35" maxlength="30" value="Description" id="idDesc4" class="bg_input txtTable" style=""> The field name is a list value. Thus passing $('#listDespRec[%{#rowStatus.index}].observacaLanking'). unbind('copy Paste'); does not work

0

Man if I understood correctly it would be something like this right?

// Não permitir o copy/paste
$('input').bind('copy paste', (e) => false);

// Permitir
$('.copy_paste_on').unbind('copy paste');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input placeholder="campo 1">
<input class="copy_paste_on" placeholder="campo 2">
<input placeholder="campo 3">

In your example is not working because this missing warn which type of the unbind follows example of unbind:

.unbind( eventType [, handler ] )

Browser other questions tagged

You are not signed in. Login or sign up in order to post.