Remove focus from input when it is with attr readonly

3

I have the following problem, I have an input with the readonly attribute. When clicked on it it is selected. I needed to prevent this because I have a function using onblu r that can only work when input is not in readonly .

<input type="text" readonly="readonly" onblur="alert('teste')">
    
asked by anonymous 25.11.2016 / 13:22

2 answers

3

You can handle this with JavaScript, Here's 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')">
    
25.11.2016 / 13:43
3

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

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

Or check if it's 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><inputid="a" type="text" readonly="readonly">
    
25.11.2016 / 13:36