Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7182909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:52:15+00:00 2026-05-28T17:52:15+00:00

I’m working on a custom dropdown that I made, just wondering if I could

  • 0

I’m working on a custom dropdown that I made, just wondering if I could get some advice as how to shorten / simplify my jQuery code. I have two lists that do the same thing and I have the code duplicated, which i know is long winded and really not a good approach.

example: http://jsfiddle.net/dg7Lc/22/

HTML:

<div class="custom-select list-one">
    <span>Select Option</span>
    <ul>
        <li>List Item 1</li>
        <li>List Item 2</li>
        <li>List Item 3</li>
        <li>List Item 4</li>
        <li>List Item 5</li>
        <li>List Item 6</li>
        <li>List Item 7</li>
        <li>List Item 8</li>
        <li>List Item 9</li>
        <li>List Item 10</li>
        <li>List Item 11</li>
        <li>List Item 12</li>
    </ul>
</div>


<div class="custom-select list-two">
    <span>Select Option</span>
    <ul>
        <li>List Item 1</li>
        <li>List Item 2</li>
        <li>List Item 3</li>
        <li>List Item 4</li>
        <li>List Item 5</li>
        <li>List Item 6</li>
        <li>List Item 7</li>
        <li>List Item 8</li>
        <li>List Item 9</li>
        <li>List Item 10</li>
        <li>List Item 11</li>
        <li>List Item 12</li>
    </ul>
</div>

JavaScript:

//Function for .list-one
$(function() {

    $('.custom-select.list-one').click(function() {
        $('.custom-select.list-one ul').slideToggle('fast');
    });

    $('.custom-select.list-one ul li').click(function() {
        $(this).addClass('selected');
        $('.custom-select.list-one span')
        .html($('.custom-select.list-one ul li.selected').html());
        $(this).removeClass('selected');
    });

});

// Function for .list-two
$(function() {

    $('.custom-select.list-two').click(function() {
        $('.custom-select.list-two ul').slideToggle('fast');
    });

    $('.custom-select.list-two ul li').click(function() {
        $(this).addClass('selected');
        $('.custom-select.list-two span')
        .html($('.custom-select.list-two ul li.selected').html());
        $(this).removeClass('selected');
    });

});

Any advice, thanks!

-Big D

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:52:16+00:00Added an answer on May 28, 2026 at 5:52 pm

    Something like the following seems to work:

    $(function() {
    
        $('.custom-select').click(function() {
            $(this).find('ul').slideToggle('fast');
        });
    
        $('.custom-select ul li').click(function() {
            var $this = $(this);
            $this.closest('div').find('span').html($this.html());
        });
    
    });
    

    The idea is that you don’t need to know which of the selects was clicked on: whichever it is, you just find its child ul element using $(this).find() and then toggle that. Similarly, when a li is clicked you just find the div element it belongs to with .closest() (you could also use .parent().parent()) and then from there find the associated span element.

    I also store a reference to the object returned by $(this) rather than recreating it each time you need it.

    I noticed you were adding a “selected” class to the clicked li element only for use as a selector immediately afterwards, and then removing it again – you don’t need it at all since you already have a reference to the clicked element in this.

    Note also that you don’t need two document.ready handlers: even for your code as you had it with the repetition you could’ve defined all of your click handlers within a single document.ready.

    Also I wouldn’t use a class (like your “list-one” and “list-two”) to uniquely identify each list: that’s what the id attribute is for.

    Demo: http://jsfiddle.net/dg7Lc/25/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.