Blur / Focus with Jquey

0

I'm doing a project that has several inputs, but I can only type in just 2 of them, so I would like my jquery to do a validation so that the user can just enter these fields, something like this:

 $("#FL1").on('blur', function () {
    if(campo == 1){
        return;
    }
    else 
        $("#FL1").focus();
 });

 $("#FL2").on('blur', function () {
    campo = 0;
 });

Is there any way to do something like this?

    
asked by anonymous 05.07.2016 / 14:50

1 answer

1

I do not know if I understand very well what you want, but if you do not want to let the user type anything in some inputs, you can use the event itself .focus () for this.

To do this, simply add the inputs that can not be typed, and in the focus event, redirect it to the main input.

An example would look like this:

$('.ignore').focus(function(e) {
  e.preventDefault();
  $('#fl1').focus();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Principal:<inputid="fl1" />
<br/>
<br/>Ignorar:
<input class="ignore" />
<br/>Ignorar:
<input class="ignore" />
<br/>Ignorar:
<input class="ignore" />
<br/>
<br/>
<br/>Secundario:
<input id="fl2" />

In this way, all inputs with the ignore class will be directed to the input with the id fl1 .

Issue

Based on the comments, we can use the same analogy, but the reverse. We'll use document as a selector, and if it's not the input's that you can access, focus on what you want, like this:

$(document).click(function(event) {
  if (!$(event.target).closest(".ignore").length) {
    $('#fl1').focus();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Principal:<inputid="fl1" class="ignore" />
<br/>
<br/>Ignorar:
<input />
<br/>Ignorar:
<input />
<br/>Ignorar:
<input />
<br/>
<br/>
<br/>Secundario:
<input id="fl2" class="ignore" />

For more information, you can look at this answer.

    
05.07.2016 / 15:18