Customizing your Webflow site with jQuery
Tags: beginners, jquery, design, webdev
Published: 2023-05-05 18:57:03

If you’re a designer or web developer and you haven’t yet given Webflow a try, you’re missing out… especially if you’re familiar with jQuery. 

## First things first. What is Webflow?

[Webflow](https://webflow.com/) is a unique website building platform used by over a million designers/developers to design modern web experiences with minimal to no code. The Webflow interface allows you to design highly customized web layouts, CSS animations, and frontend visuals in HTML, CSS and JavaScript, without actually having to write every line of code yourself. Also, Webflow has an amazing cadre of curated tutorials via [Webflow University](https://university.webflow.com/) and a supportive community of designers/developers that can be found on YouTube, blogs, and the like.

## So, why jQuery if Webflow writes the code for us?

[jQuery](https://jquery.com/) is a rich, popular JavaScript library that makes things like DOM manipulation and event handling much simpler and easier to read. Also, Webflow automatically loads jQuery into all web projects so you don’t even have to import it.

In my experience, it is totally possible to design a great website in Webflow without writing a single line of code. 

But… There are a number of instances when it’s easier, faster, and better to write just a few lines of jQuery. There might be situations where your design vision or site functionality would otherwise require a scale back without a bit of code. We’ll walk through a few Webflow-specific examples in a bit. 

But… Why jQuery when we could just write JavaScript? 

Okay, here’s a quick demo. Let’s say you want to add a ‘click’ event listener to a `<button>` element. When the button is clicked, text is added to a `<p>` paragraph element. 

As opposed to writing something like this:

```js
const paragraph = document.getElementById('appear-on-click');

document.querySelector('button').addEventListener('click', () => {
  paragraph.innerHTML = "Button has been clicked with JavaScript!"
})

```
You could write less code with arguably more readable jQuery… for instance:

```js
const $paragraph = $("#appear-on-click");

$("button").click(() => {
  $paragraph.text("Button as been clicked with jQuery!");
});

```
Again, Webflow already loads the jQuery library on every site automatically.

Before the Click Event:
![Before Click Event](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v20qjpl3nwany9eoamia.png)

After the Click Event:
![After Click Event](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5t0pyfsux5b44wkqy7o4.png)



## Disable Body Scroll with Open Menu Click
**First use case for using jQuery in Webflow:**

When creating a custom navigation menu in Webflow, there are a few features you’ll likely want to add to the menu that make the navigation experience more user friendly. One of the most common is to disable page scroll while the navigation menu is open. Here’s how you could handle that.

1. **CREATE ‘scroll-disabled’ CLASS:** Inside the Webflow designer, select the body element. You’ll want to add a class of ‘scroll-disabled’ to the body element. Then set that class to Overflow hidden. Then REMOVE that class by clicking backspace/delete within the style selector.
![Webflow Interface for setting a class](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vzbyhppcut2mf64y10t8.png) 


2. **ADD jQuery TO PAGE:** Inside the Webflow designer toolbar within the section titled ‘Before `</body>` tag’, just drop this jQuery function. Be sure to replace the placeholder class name below with the class name of your button. Just click publish in the Webflow designer, and voila! 

```js
// replace '.menu-btn-class-here' with the class name of your menu button element and use the jQuery .click method to add a ‘click’ event listener
$(".menu-btn-class-here").click(() => {
 // use jQuery .toggleClass method to add and remove the 'scoll-disabled' class on click
  $('body').toggleClass('scroll-disabled')
})
```
_FYI: Custom code changes added before the page’s closing body tag section will NOT appear in preview BUT will be made to the live staging site after being published._

## Close Menu when User Clicks Off Menu
**Second use case for using jQuery in Webflow:**

Here’s another custom navigation menu feature that is easy to fix with a few lines of jQuery. When creating a custom navigation menu, you’ll want users to be able to close the open menu when they click anywhere outside the menu itself. This provides another way for users to close the menu without actually clicking the close menu button. This really simple feature improves the user experience a ton.

1. **ADD jQuery TO PAGE:** Again… Inside the Webflow designer toolbar within the section titled ‘Before `</body>` tag’, just drop this jQuery function. Be sure to replace the placeholder class names below with your actual class names. Just click publish in the Webflow designer, and voila! 

```js
/ Again use the jQuery .click method on the element with the class name of your background div
$(".background-class-name-here").click(() => {
 // replace '.menu-btn-class-here' with the class name of your menu button element
 $(".menu-btn-class-here").click();
})
```

_FYI it’s important that your menu `<div>` element is not (nested within) a child  of the background `<div>` element as any click on or off the menu element would glitch and close the menu._

There are so many opportunities to upgrade your Webflow site with just a little bit of jQuery. Thanks for reading!

If you’re yearning for more, here are a couple of great resources that I personally reference often:

- Timothy Ricks | an amazing Webflow Developer. Here’s a [link](https://www.youtube.com/watch?v=BkoSPoXbpb4) to his YouTube tutorial on using jQuery in Webflow. 

- FinSweet | an amazing go-to for everything Webflow. Here’s a [link](https://finsweet.com/hacks-jquery) to their page with some great jQuery hacks and tutorials.