Error hiding <div> by selecting <option> in a form with javascript

Asked

Viewed 115 times

2

I have a javascript function with jquery that hides Divs when an option is selected. It works almost perfectly, but when you go back to a previous option, after choosing an option that opens div, the div that was opened before doesn’t disappear anymore (unless you choose one of the options that opens another div, then it switches).

$(function(){
    $("#motde").change(function(){
          if($(this).val()=='mostrard'){
              $('#justcausa').show();
              $('#rescind').hide();
          }
          if ($ (this).val()=='mostrarc'){
              $ ('#rescind').show();
              $('#justcausa').hide();
          }
          if ($ (this).val()=='nada') {
              $ ('rescind').hide();
              $ ('justcausa').hide();
          }
     });
});

I put in jsfiddle to facilitate understanding: https://jsfiddle.net/gustavox/u9avs55f/3/

What happens is that if you select options 2 or 3, it opens and changes the Divs, but if you choose another option later (changes the option, to one that does not open div), it does not make the Divs disappear. For better understanding, select option 2, and then option 4. The open div because of option 2 should disappear, only this does not happen.

Why?

1 answer

2


Missing line 12 and 13 # to indicate id, change:

 $ ('rescind').hide();
 $ ('justcausa').hide();

for:

 $ ('#rescind').hide();
 $ ('#justcausa').hide();
  • OMG! Close topic, ban op! : ) Thanks!

Browser other questions tagged

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