Addclass and Removeclass do not perform as expected

Asked

Viewed 241 times

0

I have a select that when changing to a certain value you should remove the class col-md-9 and add the class col-md-4 to fit a new field on the screen. He should stand as the example.

Como deve ficar

But when I change to the desired select it has no effect.

The strange thing is that I do the same with another field (when put in another value of select) and it works normally (being done in the same way, changing the name of the obvious fields).

$(document).ready(function () {
     $('#input-produto').change(function () {
         matricula_produto();
     });


    function matricula_produto() {
        let val = $('#inputs_socios option:selected').attr('pcasal');
        if (val == 1) {
           $('.input-produto').addClass('col-md-4').removeClass('col-md-12');
           $('.input-vinculo').insertAfter($('.input-produto')).show();
        } else {
        $('.input-produto').addClass('col-md-12').removeClass('col-md-4');
        $('.input-vinculo').hide();
        }
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">

<div class="form-group input-produto col-md-12">
   <label for="inputs_socios">Produto*</label>
   <select name="inputs_socios" id="inputs_socios" class="form-control">
      <option value="">Escolha</option>
      <option pcasal="0">SÓCIO 1</option>
      <option pcasal="1">MUDAR</option>
      <option pcasal="0">SÓCIO 3</option>
   </select>
</div>

<div class="form-group col-md-6 input-vinculo" style="display:none">
   <label for="seq_vinculo">Vinculado</label>
   <select name="seq_vinculo" id="seq_vinculo" class="form-control">
      <option pcasal="0">TESTE</option>
      <option pcasal="1">TESTE2</option>
      <option pcasal="0">TESTE3</option>
   </select>
</div>

If I turn the remote $('.input-produto').addClass('col-md-4').removeClass('col-md-9'); on the console it works, but it is not running at the time it needs to. But the other value is. And it is entering the if as the field is appearing, just not adding and removing the class.

  • Can you build a [mcve] to demonstrate the problem? The site’s Snippet has the option to add jQuery.

  • @Andersoncarloswoss I added the snippet.

  • William, this jQuery code shouldn’t be inside an event, like change, of <select>? The way it is will be executed when the page loads, not when the value is changed.

  • In the code it is inside a function that checks every time it is changed. So much so that as I mentioned in the post another field that is practically a Ctrl+c Ctrl+v has the expected behavior with this code.

  • And if I copy the code and paste it into the console it works as it should.

  • Then add this excerpt also to the example, otherwise it will not accurately reproduce your problem.

  • It is not possible to add this because the function is too large, and calls other functions that ends up getting confused to add only that part.

  • 2

    William, but that’s exactly what the MCVE is for. By making an example that reproduces the error, you ensure that the error is in the code you presented to us. To say that it is not possible to reproduce because there is a lot of code that is not in the question opens up a huge margin for the error to be precisely in the part you omitted. If the minimum example (as shown in the answer) works, then surely the error is in some other part of the code, which makes your question insufficiently clear and impossible to answer.

  • The code itself pertaining to this is just a change.

  • Then add the part that deals with the event change in the question to elaborate the necessary [mcve].

  • I just added .

Show 6 more comments

1 answer

1


Hi there, get

 $(document).ready(function(){

to wait for the code to be rendered to run html.

and add the event

 $('#inputs_socios').on("change", function(){

so that when every time select is changed it will perform the action you want, as an example below :

$(document).ready(function(){
      $('#inputs_socios').on("change", function(){
          let val = $('#inputs_socios option:selected').attr('pcasal');
        if (val == 1) {
            $('.input-produto').addClass('col-md-4').removeClass('col-md-12');
            $('.input-vinculo').insertAfter($('.input-produto')).show();
        } else {
            $('.input-produto').addClass('col-md-12').removeClass('col-md-4');
            $('.input-vinculo').hide();
        }
      });

   
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">

<div class="form-group input-produto col-md-12">
   <label for="inputs_socios">Produto*</label>
   <select name="inputs_socios" id="inputs_socios" class="form-control">
      <option value="">Escolha</option>
      <option pcasal="0">SÓCIO 1</option>
      <option pcasal="1">MUDAR</option>
      <option pcasal="0">SÓCIO 3</option>
   </select>
</div>

<div class="form-group col-md-6 input-vinculo" style="display:none">
   <label for="seq_vinculo">Vinculado</label>
   <select name="seq_vinculo" id="seq_vinculo" class="form-control">
      <option pcasal="0">TESTE</option>
      <option pcasal="1">TESTE2</option>
      <option pcasal="0">TESTE3</option>
   </select>
</div>

  • Without success. had already done something similar.

  • What puzzles me is that if I paste the code into the console it works.

  • works, because by the time you executed the code the page had already been fully loaded, however in the application it runs immediately.

  • How to solve this.

  • Applying $(Document). ready(Function(){ as I said and did in the reply

  • If you run the snippet code you will see that it works, if in your case it is not working, it is because there are codes that are not in your question, please supplement the question with the remaining code.

  • It’s like that already @Marcianomachado

  • But the snippet doesn’t work, it’s just running the show and not the addClass() and the removeClass()

  • Your select id name is wrong in one place you are using #inputs_socios and another #input-product

  • Oops, I’ve been over here and I’ve found the problem, it’s logical. What happens is that he is running the addClass and Remove, but then he ends up falling into a Lse down there that checks other things and removing it. With debbuger I found the problem. Thank you so much for taking your time to try to help me.

Show 5 more comments

Browser other questions tagged

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