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?
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.
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.
Here is a fiddle to prove the theory