Autofocus and Required in javascript hidden fields

Asked

Viewed 284 times

1

I have a field to select two other fields. When shows the situation field hides the other div and vice versa, this is working perfectly. I wonder how I can do to give a required autofocus when the selected field is visible, because I already tried to put the direct command in the div and it happens that with this I use the search button, Because, as the two camps are requesting something written ends up not working. Just follow my code.

 <!--Script que esconde e mostra Divs-->
        <script type="text/javascript">
            window.onload = function(){
                id('tipo').onchange = function(){
                    if( this.value=="" || this.value=="contrato" || this.value=="cliente" || this.value=="cpf" || this.value=="numero" || this.value=="solicitante" )
                        id('filtro').style.display = 'block';
                    else
                        id('filtro').style.display = 'none';

                    if( this.value=="situacao" )
                        id('seletor').style.display = 'block';
                    else
                        id('seletor').style.display = 'none';
                }
            }
            function id(el){
                return document.getElementById(el);
            }
        </script>
        <!--Fim Script que esconde e mostra Divs-->

        <!--Campo Tipo-->
        <form method="get" action="pesquisas.php">
            <div class="row">
                <div class="col-sm-5">
                    <div class="input-group col-lg-12">
                        <div class="input-group-addon">
                            <span class="glyphicon glyphicon-search"></span>
                        </div>
                        <select required autofocus class="form-control" name="tipo" id="tipo">
                            <option value="">SELECIONAR</option>
                            <option value="situacao">SITUAÇÃO</option>
                            <option value="contrato">CONTRATO</option>
                            <option value="cliente">CLIENTE</option>
                            <option value="cpf">CPF</option>
                            <option value="numero">NÚMERO</option>
                            <option value="solicitante">SOLICITANTE</option>
                        </select></form></div></div>
<!--Fim Campo Tipo-->

<!--Campo Situaçao-->
<div class="col-sm-2 col-lg-4" >
    <select type="text" class="form-control" name="seletor" id="seletor" style="display: none">
        <option value="">SELECIONAR</option>
        <option value="solicitada">SOLICITADA</option>
        <option value="portada">PORTADA</option>
        <option value="cancelada">CANCELADA</option>
        <option value="erro">ERRO</option>
    </select>
</div>
<!--Fim Campo Situaçao-->

<!--Campo de pesquisa escrita-->
<div class="col-sm-2 col-lg-4" >
    <input type="text" class="form-control" name="filtro" id="filtro"></div>
<!--Fim Campo de pesquisa escrita-->

<input type="submit" value="Pesquisar" class="btn btn-primary"><br><br>

2 answers

0

I believe you can use the focus() and the setAttribute("required", ""), to make the SITUATION field be required and focused when it appears on the screen. Check below:

window.onload = function(){
                id('tipo').onchange = function(){
                    if( this.value=="" || this.value=="contrato" || this.value=="cliente" || this.value=="cpf" || this.value=="numero" || this.value=="solicitante" ) {
                        id('filtro').style.display = 'block';
                        }
                    else {
                        id('filtro').style.display = 'none';
                        }

                    if( this.value=="situacao" ) {
                        id('seletor').style.display = 'block';
                        id('seletor').focus();
                        id('seletor').setAttribute("required", "");
                        }
                    else {
                        id('seletor').style.display = 'none';
                        }
                }
            }
            function id(el){
                return document.getElementById(el);
            }
<form method="get" action="pesquisas.php">
            <div class="row">
                <div class="col-sm-5">
                    <div class="input-group col-lg-12">
                        <div class="input-group-addon">
                            <span class="glyphicon glyphicon-search"></span>
                        </div>
                        <select required autofocus class="form-control" name="tipo" id="tipo">
                            <option value="">SELECIONAR</option>
                            <option value="situacao">SITUAÇÃO</option>
                            <option value="contrato">CONTRATO</option>
                            <option value="cliente">CLIENTE</option>
                            <option value="cpf">CPF</option>
                            <option value="numero">NÚMERO</option>
                            <option value="solicitante">SOLICITANTE</option>
                        </select></form></div></div>

<div class="col-sm-2 col-lg-4" >
    <select type="text" class="form-control" name="seletor" id="seletor" style="display: none">
        <option value="">SELECIONAR</option>
        <option value="solicitada">SOLICITADA</option>
        <option value="portada">PORTADA</option>
        <option value="cancelada">CANCELADA</option>
        <option value="erro">ERRO</option>
    </select>
</div>

<div class="col-sm-2 col-lg-4" >
    <input type="text" class="form-control" name="filtro" id="filtro"></div>

<input type="submit" value="Pesquisar" class="btn btn-primary"><br><br>

0


When the widget is visible, set the attribute required as true; when invisible, define as false.

The autofocus will only work when loading the page, so use the method .focus() to focus on the element.

I made a small change to the structure of your HTML because the submit was outside the form and had some divs closed so wrong.

Behold:

window.onload = function(){
   id('tipo').onchange = function(){
      if( this.value=="" || this.value=="contrato" || this.value=="cliente" || this.value=="cpf" || this.value=="numero" || this.value=="solicitante" )
         id('filtro').style.display = 'block',
         id('filtro').required = true,
         id('filtro').focus();
      else
         id('filtro').style.display = 'none',
         id('filtro').required = false;
   
      if( this.value=="situacao" )
         id('seletor').style.display = 'block',
         id('seletor').required = true,
         id('seletor').focus();
      else
         id('seletor').style.display = 'none',
         id('seletor').required = false;
   }
}

function id(el){
   return document.getElementById(el);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">

<form method="get" action="pesquisas.php">
   <div class="row">
      <div class="col-sm-5">
         <div class="input-group col-lg-12">
            <div class="input-group-addon">
               <span class="glyphicon glyphicon-search"></span>
            </div>
            <select required autofocus class="form-control" name="tipo" id="tipo">
               <option value="">SELECIONAR</option>
               <option value="situacao">SITUAÇÃO</option>
               <option value="contrato">CONTRATO</option>
               <option value="cliente">CLIENTE</option>
               <option value="cpf">CPF</option>
               <option value="numero">NÚMERO</option>
               <option value="solicitante">SOLICITANTE</option>
            </select>
         </div>
      </div>
   </div>
   <!--Fim Campo Tipo-->
   
   <!--Campo Situaçao-->
   <div class="col-sm-2 col-lg-4" >
      <select type="text" class="form-control" name="seletor" id="seletor" style="display: none">
         <option value="">SELECIONAR</option>
         <option value="solicitada">SOLICITADA</option>
         <option value="portada">PORTADA</option>
         <option value="cancelada">CANCELADA</option>
         <option value="erro">ERRO</option>
      </select>
   </div>
   <!--Fim Campo Situaçao-->
   
   <!--Campo de pesquisa escrita-->
   <div class="col-sm-2 col-lg-4" >
      <input type="text" class="form-control" name="filtro" id="filtro">
   </div>
   <!--Fim Campo de pesquisa escrita-->
   
   <input type="submit" value="Pesquisar" class="btn btn-primary"><br><br>
</form>

A slightly more optimized form of the script:

window.onload = function(){
   id('tipo').onchange = function(){
      var filtro = id('filtro');
      var seletor = id('seletor');

      if( !this.value || this.value.match(/^(contrato|cliente|cpf|numero|solicitante)$/) )
         filtro.style.display = 'block',
         filtro.required = true,
         filtro.focus();
      else
         filtro.style.display = 'none',
         filtro.required = false;

      if( this.value=="situacao" )
         seletor.style.display = 'block',
         seletor.required = true,
         seletor.focus();
      else
         seletor.style.display = 'none',
         seletor.required = false;
   }
}

function id(el){
   return document.getElementById(el);
}

Using jQuery

$(document).ready(function(){
   $("#tipo").change(function(){
      var filtro = $('#filtro');
      var seletor = $('#seletor');
      
      if( !$(this).val() || $(this).val().match(/^(contrato|cliente|cpf|numero|solicitante)$/) )
         filtro
         .show()
         .attr('required', true)
         .focus();
      else
         filtro
         .hide()
         .attr('required', false);

      if( $(this).val() == "situacao")
         seletor
         .show()
         .attr('required', true)
         .focus();
      else
         seletor
         .hide()
         .attr('required', false);
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">

<form method="get" action="pesquisas.php">
   <div class="row">
      <div class="col-sm-5">
         <div class="input-group col-lg-12">
            <div class="input-group-addon">
               <span class="glyphicon glyphicon-search"></span>
            </div>
            <select required autofocus class="form-control" name="tipo" id="tipo">
               <option value="">SELECIONAR</option>
               <option value="situacao">SITUAÇÃO</option>
               <option value="contrato">CONTRATO</option>
               <option value="cliente">CLIENTE</option>
               <option value="cpf">CPF</option>
               <option value="numero">NÚMERO</option>
               <option value="solicitante">SOLICITANTE</option>
            </select>
         </div>
      </div>
   </div>
   <!--Fim Campo Tipo-->
   
   <!--Campo Situaçao-->
   <div class="col-sm-2 col-lg-4" >
      <select type="text" class="form-control" name="seletor" id="seletor" style="display: none">
         <option value="">SELECIONAR</option>
         <option value="solicitada">SOLICITADA</option>
         <option value="portada">PORTADA</option>
         <option value="cancelada">CANCELADA</option>
         <option value="erro">ERRO</option>
      </select>
   </div>
   <!--Fim Campo Situaçao-->
   
   <!--Campo de pesquisa escrita-->
   <div class="col-sm-2 col-lg-4" >
      <input type="text" class="form-control" name="filtro" id="filtro">
   </div>
   <!--Fim Campo de pesquisa escrita-->
   
   <input type="submit" value="Pesquisar" class="btn btn-primary"><br><br>
</form>

  • Friend even know how to thank. It was exactly what I killed the head to understand. I am new in programming. Mto thanks.

  • is nois on tape +1. When the second select appears by clicking situation, the word of that second select is cropped

  • @Leocaracciolo opa! rsrs.. This has to do with the Bootstrap class he’s using. I didn’t go into this detail because it doesn’t have much to do with the question.

  • That’s what I thought, you’re too lazy! kkk

  • @Carloskhan It is very important for the community that you mark the answer that most served you with (if you do not know how to do this, take a look on this page of this link). By marking the answer (only ONE of them) you are not only respecting the way the site works, but also encouraging and rewarding those who strive to answer your questions. Abs!

Browser other questions tagged

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