JavaScript Hide/Show part of the form on click. How to debug? show hide part of text jquery,css-only hide/show

I'm learning JS/Jquery and I'm trying to make a part of a form show and hide when clicking on part of the form. I am able to open part of the form, but not to close it again.

As there is no error showing in the console. I was wondering how people would go about debugging this type of bug?

The form:

<div class="col col-sm-4 more-options">Detailed options</div>

<div class="row advanced-options">
  #part of the form to open and close
</div>

The JS/jQuery code:

$(".more-options").click(function(){
    if ('.advanced-options:hidden') {
        $('.advanced-options').show();
    } else {
        $('.advanced-options').hide();
    }
});
Answer:1

So what you have in your code is basically every single time truthy value for your if statement.

Imagine the following scenario:

if ('.advanced-options:hidden') {
  console.log(`I'm true`);
} else {
  console.log(`I won't be showed up anywhere`);
}
Answer:2

I have change some code:

I have added on "$" sign in if condition, and in condition i have checked length of element. And this is not stupid thing just your beginning, so enjoy the code.

<!DOCTYPE html>
<html>
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
   </head>
   <body>
      <div class="col col-sm-4 more-options">Detailed options</div>
      <div class="row advanced-options">
         #part of the form to open and close
      </div>
      <script>
         $(document).ready(function(){
          $(".more-options").click(function(){

             if ($('.advanced-options:hidden').length > 0) {
               $('.advanced-options').show();
             } else {
               $('.advanced-options').hide();
             }
           });
         });
      </script>
   </body>
</html>
Answer:3

The part if ('.advanced-options:hidden') due to the condition is a string which gets interpreted as true. Did you mean if ($('.advanced-options:hidden')) ?

Answer:4

To answer your actual question: I would use the javascript function console.log() to provide myself with logging output.

In your case for example:

$(".more-options").click(function(){
    console.log('clicked!');
    if ('.advanced-options:hidden') {
      console.log('reached the first condition);
      $('.advanced-options').show();
    } else {
      console.log('else was called');
      $('.advanced-options').hide();
    }
  });

And in the concrete example, you just have a string in your if-clause, which would always evaluate to true. You are probably meaning to do something like $('.advanced-options:hidden'), but that's quite inefficient. In this case you would want to get the element in question and then check its properties.

Answer:5

I'm trying to correctly orient an image uploaded to a canvas from Apple devices. Here's a sample of my code to correctly orient something with EXIF orientation 6 ctx.rotate(0.5 * Math.PI); ctx....

I'm trying to correctly orient an image uploaded to a canvas from Apple devices. Here's a sample of my code to correctly orient something with EXIF orientation 6 ctx.rotate(0.5 * Math.PI); ctx....

  1. canvas rotate translate
  2. canvas rotate transform
  3. js canvas rotate translate
  4. canvas scale rotate translate
  5. html canvas translate rotate
  6. javascript canvas translate rotate

I am trying to convert form data into json I have a html table as following. On form submit what i want to do is convert it to json <form class="sales-order-form"> <table class="table"&...

I am trying to convert form data into json I have a html table as following. On form submit what i want to do is convert it to json <form class="sales-order-form"> <table class="table"&...

Here is what I have already: myFunct({ myObj: { db } }) I need to add another function in such as: myFunct({ myObj: async ({ req }) => { //more scripts } }) What I tried and failed: myFunct({...

Here is what I have already: myFunct({ myObj: { db } }) I need to add another function in such as: myFunct({ myObj: async ({ req }) => { //more scripts } }) What I tried and failed: myFunct({...

  1. declaring async function javascript
  2. declaring async function
  3. declare async function typescript
  4. declare async function js
  5. declare async function nodejs
  6. declare async function es6
  7. declare async function react
  8. declare async function c#
  9. declare async arrow function
  10. node declare async function
  11. typescript declare async function type
  12. ts declare async function

I have a HTML select, and using the Chosen JS plugin, for multiple select. Chosen uses the CSS property background-image and others to set the style for each select option (always the same style). ...

I have a HTML select, and using the Chosen JS plugin, for multiple select. Chosen uses the CSS property background-image and others to set the style for each select option (always the same style). ...

  1. possible different colors