Hi Team
I have a dropdownlist of popular,latest and best buying category list. Now how to i reference these to jquery so when user if select popular the cart-container has an image of those category appears, same applies with other from the dropdownlist?
// html code
Code
Sort by
Latest
Popularity
Best Rating
Naimish MakwanaRank #324 · 7.7K reputationPosted May 8, 2023, 12:27 AM EDT
You can use jQuery to listen for the
clickevent on each of the dropdown items and then display the corresponding image based on the selected option. Here's an example:HTML:
jQuery:
$(document).ready(function() { // listen for click event on dropdown items $('.dropdown-item').on('click', function() { // get the selected category var category = $(this).data('category'); // remove any existing images in the cart container $('#cart-container').empty(); // display the corresponding image if (category == 'latest') { $('#cart-container').append(''); } else if (category == 'popular') { $('#cart-container').append(''); } else if (category == 'best') { $('#cart-container').append(''); } }); });In this example, we've added a
data-categoryattribute to each dropdown item to store the corresponding category. Then, in the jQuery code, we listen for theclickevent on the dropdown items and retrieve the selected category using thedatamethod. We then remove any existing images in the cart container using theemptymethod and display the corresponding image using theappendmethod.Thanks