/*wp_block_styles_on_demand_placeholder:69aad21b49194*/ /*# sourceURL=wp-block-library-inline-css */

Change a select option using jQuery in Divi

How to dynamically change a select option in a dropdown using jQuery

Here is a nifty trick – if you have a select dropdown but want to have something a bit nicer in appearance and change the value of this drop down dynamically. Not sure what I am talking about?

Scenario 1: You have a WooCommerce website and have variations, these are in a select drop down. Maybe you want to have images for each variation and when the customer clicks on this image, it changes the variation to add to cart.

Scenario 2: You have a select drop down to filter posts but you want a nice image instead.

Both of these scenarios are not the only applications but you get the idea of what you can do with this quick bit of code.

Step 1: Create a select dropdown

Below is a bit of code that will create the select dropdown – for testing purposes we have simply added this dummy code. We will show you a real life application after.

 

Step 2: Add target to change the select option

The next step is to add whatever you want that will change the select value. This could be an image, icon, blurb, button or anything else you can think will work. For our purposes, we will use a blurb module. Go ahead and add your blurb module, upload a nice image or use an icon.

There is one key thing you need to do here, which is to add to the blurb module an ID that corresponds with the value of the select. In our example we have “web-design” as a value in the select option. We give one of the blurbs an ID of “web-design”, this way when they click this, it will tell our jQuery code which to change to. See the image below (1 = the value you want it to change)

One final thing we need to do here is to add the ID “custom-grid-control” to our row or section that the blurbs are in. This way we can target just these blurbs using jQuery.

change-select-otion-with-jquery

Step 3: Add jQuery

The final step is to add the jQuery – we have added comments next to each line to tell you what it does – also watch the video at the bottom of this post which we explain it all in-depth if you need more help.

Step 4: Fine tune

And that’s it, now when you click on the blurb with the ID “web-design”, it will change the select to be Web Design, likewise, if you click on a blurb with an ID “social” it will change the select option to be the Social option.

Live example

change select dropdown using jquery

Now we understand how we can do this, let’s show you a live example. We are (at the time of writing) working on a new layout pack for our plugin Divi Machine for projects. On this layout we decided to use this code. We have some projects and using Divi Machine, filtering them based on their categories.

We have set up the archive loop module and the filter module. In the filter module, we have specified just the categories to be an option to filter (make sure you select the option to update on the change field rather than the button). Now this is all set up we can filter our projects.

This is great but maybe we want to use images instead, we can use the technique above to do this. Add some blurbs to the page and make sure you give each one the ID that corresponds to the option value (the category slug). Now we have done this, add the jQuery code and when you click on the blurb, it will update the select dropdown in the filter and then filter the posts.

One thing to think of is in our example above we have added a div called “custom-filter-posts” around our dummy select dropdown – we need to make sure to add this same CSS class to our filter module in the CSS section.

We have also added some extra CSS to make things a little bit better (see below with comments)

Check the video below for more details on this all

2 Comments

  1. thank you for this great tutorial, is there an option to disable Blurbs while its loading /filtering ?
    Thx for you answer 🙂

    • Kind of hard to do this as there is not a way to know when it is loaded – it should not take too long to load the content?

Submit a Comment

Explore more from Divi Engine

Divi Form Builder

Divi Form Builder

From simple contact forms to complex frontend post or product creation, Divi Form Builder has you covered.

Divi Form Builder
Find out more
Divi Machine

Divi Machine

Build complex websites that displays dynamic fields you can filter, search and so much more with the Divi Builder.      

Divi Machine
Find out more
Divi BodyCommerce

Divi BodyCommerce

A versatile toolkit for developers using Divi and WooCommerce together, designed to boost your e-commerce site and achieve greater conversion rates.    

Divi BodyCommerce
Find out more
Divi Handoff

Divi Handoff

Let clients update content without touching the Divi Builder. Design layouts once, connect them to Flexible Content fields, and give editors simple forms for text, images, and sections.

Divi Handoff
Find out more
Divi Loop Extender

Divi Loop Extender

Unlock the Full Power of Divi 5 Loop Builder Add advanced sorting, filtering, and relationship logic right inside the Visual Builder.

Divi Loop Extender
Find out more
Divi Membership

Divi Membership

Monetize your Divi websites by transforming them into membership sites with seamless subscription management, user-friendly interfaces, and customizable membership tiers.

Divi Membership
Find out more
Divi Machine Accounts

Divi Machine Accounts

Build an account area for your customers to edit their details, access wishlist, submitted posts and more. *Note: Requires Divi Machine installed and active

Divi Machine Accounts
Find out more
Divi Ajax Filter

Divi Ajax Filter

Filter WooCommerce, Posts & Custom Posts without reloading the page.    

Divi Ajax Filter
Find out more
Divi Mobile

Divi Mobile

Divi Mobile helps you create beautiful looking mobile menus without having to code.

Divi Mobile
Find out more
Divi Nitro

Divi Nitro

Give your Divi website that extra boost of speed with our Divi Nitro plugin to enhance your customer's experience.

Divi Nitro
Find out more
Divi Protect

Divi Protect

Password protect the content of your Divi website with our Divi Protect plugin. Keep unwanted eyes out!

Divi Protect
Find out more
Divi Mega Menu

Divi Mega Menu

Create stunning, responsive, and content-rich mega menus using the Divi Builder you already know. No coding required.  

Divi Mega Menu
Find out more