Skip to content
Advertisement

Jquery onclick method is not working properly

I’m trying to make a search form. I want it to be like when I click in the search form opener button, the search form would show and its width would be 100%.

When I click it again the search form width would return to 0. The first one is working but when I click it back, the form is not set to width of 0.

app.js :

JavaScript

Advertisement

Answer

Simple way is to use addClass() and removeClass() functions instead of using .css Then for if statement you can use if($(selector).hasClass())

JavaScript
JavaScript
JavaScript

Explanation:

  • Add/remove class strech to the element on click

  • Check for strech class by using .hasclass('strech')) . The ! mark means NOT so !$(selector).hasClass('strech') means if the element doesn’t have this class

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement