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 4608442
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:50:44+00:00 2026-05-22T00:50:44+00:00

I have an indented dropdownlist, where I have some root options and their children,

  • 0

I have an indented dropdownlist, where I have some root options and their children, like below:

Food
    Market
    Restaurants
    Grossery
Clothes
Home
    TV

If I select Market, for example, the text in the dropdownlist still indented. Then I did a jQuery function to remove the spaces before the text. It looks like this:

$(function () {
    $("select").change(function () {
        var str = jQuery.trim($("select option:selected").text());

        $("select option:selected").text(str);
    })
}); 

It works. But, if I try to select other option after selected Market, for example, The list looks like this:

Food
Market
    Restaurants
    Grossery
Clothes
Home
    TV

Market lost its indentation. I would like a way to remove the spaces but only in the selected text shown in the dropdown, but not in the option.

What should I do?

  • 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-22T00:50:44+00:00Added an answer on May 22, 2026 at 12:50 am

    Bit late to the party here…

    First of all I’ve amended your HTML to include a class on each option element to indicate the level to which it should be indented.

    <select class="select">
        <option value="1" class="level-0">Item 1</option>
        <option value="2" class="level-1">Item 1.1</option>
        <option value="3" class="level-2">Item 1.1.1</option>
        <option value="4" class="level-1">Item 1.2</option>
    </select>
    

    I have also written the jQuery to add the required indentation on load, using a string of non-breaking spaces. While this is hardly an elegant solution, it’s the only one that will work across all browsers – as you’ve obviously discovered, OPTION elements are a land CSS forgot. It also includes the logic for the change event to remove/add the padding to the selected items.

    While it’s not the prettiest code, and I’m sure there are a lot of performance improvements which could be made (hey, it’s late here and this was a brain dump as I was intrigued by the question), it works.

    var levelClassPrefix = "level-";
    var indentationString = "&nbsp;&nbsp;&nbsp;&nbsp;";
    
    $(".select").each(function() {
        padOptions(this);
    });
    
    function padOptions(elem) {
        $("OPTION", elem).each(function() {
            var level = $(this).attr("class").replace(levelClassPrefix, "");
            var currentText = $(this).html();
            var regex = new RegExp(indentationString , "g");
            $(this).html(padText(currentText.replace(regex, ""), level))
        });        
    }
    
    function padText(value, level) {
        var output = "";
        for (var i = 1; i <= level; i++) {
            output = output + indentationString;
        }
        return output + value;
    }
    
    $(".select").change(function() {
        padOptions(this);
    
        var selectedOption = $("option:selected", this);
        var currentText = selectedOption .html();
        var regex = new RegExp(indentationString , "g");
        selectedOption.text(currentText.replace(regex, ""));
    });
    

    Here is a fiddle to prove the theory

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

Sidebar

Related Questions

I have a UITableView where some rows are indented using indentationLevel. Cells have the
I have a subroutine in Perl that should be indented like this: sub GetFiles
I have two images like so. (granted that the code is indented) @Html.Image(/Images/icons/arrow-up.gif, up)
I have a string that represents a non indented XML that I would like
I have some code like: try: do_the_first_part() except SomeError: do_the_next_part() I get an error
I have some XML chunk returned by DOMDocument::saveXML() . It's already pretty indented, with
I have some PHP code like the following (simplified): <ul> <?php for ($Index =
How to make CITE tag entirely indented? I have tested all of the padding-left:
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
I have an indented JSON string e.g. { a: 1 } However, I don't

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.