Error hiding field with Jquery

Asked

Viewed 39 times

0

I have an html code and I would like to hide a field when selecting the "Graph" option (when the field is visible I need it to be configured "required autofocus") and tried with jquery, however, unsuccessfully.

HTML

<div class="col-sm-3">TIPO:
<div class="input-group col-lg-10">
<div class="input-group-addon">
<span class="glyphicon glyphicon-search"></span>
</div>
<select required autofocus name='tipo_r' id='tipo_r' class="form-control">
<option value="LISTA">LISTA</option>
<option value="GRAFICO">GRÁFICO</option>
</select>
</div>
</div>		

<div id="ocultar" class="col-sm-3">OPERADORA:
<div class="input-group col-lg-10">
<div class="input-group-addon">
<span class="glyphicon glyphicon-search"></span>
</div>
<select class="form-control" id="op" name="op">
<option value="">SELECIONAR</option>
<option value="OP1">OP1</option>
<option value="OP2">OP2</option>
</select>
</div>
</div>

JAVASCRIPT

$(document).ready(function(){
$("#tipo_r").change(function(){
var valor = $('#op');
if( $(this).val() == "GRAFICO")
valor
.show()
.attr('required', true)
.focus();
else
valor
.hide()
.attr('required', false);								
});
});

1 answer

0


First your field that has to be hidden must already start hidden, so put display:none in the CSS

inserir a descrição da imagem aqui

If you don’t want to use CSS just put var valor = $('#op'); valor.hide(); before function change(), to get him hidden

var div = $('#ocultar');
div.hide();

$(document).ready(function() {
  var valor = $('#op');
  valor.hide();
  $("#tipo_r").change(function() {
    if ($(this).val() === "GRAFICO") {
      valor.show().attr('required', true).focus();
      div.show()
    } else {
      valor.hide().attr('required', false);
      div.hide()
    }
  });
});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

  <div class="col-sm-3">TIPO:
    <div class="input-group col-lg-10">
      <div class="input-group-addon">
        <span class="glyphicon glyphicon-search"></span>
      </div>
      <select required autofocus name='tipo_r' id='tipo_r' class="form-control">
        <option value="LISTA">LISTA</option>
        <option value="GRAFICO">GRÁFICO</option>
      </select>
    </div>
  </div>

  <div id="ocultar" class="col-sm-3">OPERADORA:
    <div class="input-group col-lg-10">
      <div class="input-group-addon">
        <span class="glyphicon glyphicon-search"></span>
      </div>
      <select class="form-control" id="op" name="op">
        <option value="">SELECIONAR</option>
        <option value="OP1">OP1</option>
        <option value="OP2">OP2</option>
      </select>
    </div>
  </div>

  • Thank you very much for the answer, but it’s like he’s already visible and then hidden?

  • @Carlos made an issue in reply, look at the JS now as it turned out, the select Hide has to be done before the change()

  • Excellent turned out all right.

  • I have a question. How can I hide a <div> with this field? Actually this field comes with a div. I will edit my code.

  • What I would like to do is hide every element understood by <div> and when it is visible <select> is configured with required autofocus.

  • @Carlos I saw that you edited, but which div you want to hide? Just put an ID in the div and put before the . change so $("#ID-da-sua-div").hide() or you put the ID that was in the hidden select in the div you want...

  • @Carlos if you do not succeed with the above hint tells me that I edit the answer

  • Ready I edited the id is id="hide". As I am new to javascript I don’t have much idea how to do. Mto thanks for the help.

  • @Carlos is there a look includes the new HTML in the answer and adjusted the code by putting var div = $('#ocultar');&#xA; div.hide(); and including show and tb within the function . (change) if your intention is even to learn this simple code can help you in a lot of things, and looking at it calmly is not difficult to understand what has been done... And if you repair it no longer need to hide and shows Select since it is inside Div ;), if you want you can remove that part of the code

  • Thanks for your patience and help. I learned a lot from you. It all worked out. Thanks.

  • @Carlos save the code can help you in the future rss, but mainly, study pure JS and forget jQuery...

Show 6 more comments

Browser other questions tagged

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