Remove Focus from input when it is with attr readonly

Asked

Viewed 366 times

3

I have the following problem, I have a input with the attribute readonly. When you click on it it is selected. I needed to prevent this because I have a function using the onblucan only work when the input is not in readonly.

<input type="text" readonly="readonly" onblur="alert('teste')">

2 answers

4


You can treat this with Javascript, follows a solution for you:

function minhafuncao(teste){
  if(document.getElementById('idinput').readOnly==false){
    //seu comando
    alert(teste);
  }
}
<input type="text" id="idinput" readonly onblur="minhafuncao('teste')">

  • This solution served me 100% thanks

3

You can use disabled="true", and when removing readonly also enable input.

<input type="text" disabled="true" readonly="readonly" onblur="alert('teste')">

Or check if it is as readonly in Blur with jQuery:

$("#a").on("blur",function(){
  if ($(this).attr('readonly') == 'readonly'){
    console.log("Somente leitura");
  } else{
    console.log("Habilitado");  
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="a" type="text" readonly="readonly">

  • So Lucas, in this case I can’t use disabled to avoid not sending the field.

  • 1

    You can check if you are readonly in the function as suggested in the second option.

  • I understood, really a solution, thank you.

Browser other questions tagged

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