arrangement of columns within the <select> element

I have been attempting to create a column layout inside of <select> using CSS, but so far I have been unsuccessful.

https://i.sstatic.net/ltIDl.png

This is the HTML code I have tried:

<select id="select-bill-period">
    <option>Select a bill period</option>
    <option><span>Current bill</span>&nbsp;&nbsp;<span>&pound;21.99</span></option>
    <option><span>20 Oct 2015</span>&nbsp;&nbsp;<span>&pound;45.99</span>&nbsp;&nbsp;<span>Archived</span></option>
    <option><span>20 Oct 2015</span>&nbsp;&nbsp;<span>&pound;45.99</span>&nbsp;&nbsp;&nbsp;<span>Archived</span></option>
    <option><span>20 Oct 2015</span>&nbsp;&nbsp;<span>&pound;45.99</span>&nbsp;&nbsp;&nbsp;<span>Archived</span></option>
</select>

Using &nbsp; is not a practical solution for me, as we are unable to use <span> or any other tag within the <option> tag. Is there any way to achieve the column structure within the <select> box?

Answer №1

var spacesToAdd = 2;
var biggestLength = 0;
$("#timezones option").each(function(){
var len = $(this).text().length;
    if(len > biggestLength){
        biggestLength = len;
    }
});
var padLength = biggestLength + spacesToAdd;
$("#timezones option").each(function(){
    var parts = $(this).text().split('+');
    var strLength = parts[0].length;
    for(var x=0; x<(padLength-strLength); x++){
        parts[0] = parts[0]+' '; 
    }
    $(this).text(parts[0].replace(/ /g, '\u00a0')+''+parts[1]).text;
});
select{
    font-family:"Courier New", Courier, monospace
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<select name="timezones" id="timezones">
    <option value="1">Current bill +21.99</option>
    <option value="2">20Jan 2016 +20.54</option>
     <option value="3">20Dec 2018 +102.65</option>
    <option value="3">20Nov 2016 +502.62</option>
</select>

Answer №2

To accomplish this task, utilize jQuery. I came across a jQuery extension that can assist you in developing a solution tailored to your needs. Click on the link provided below for more details:

multi-column selection box

Answer №3

This answer is the result of following my own structure, with a little help from @Roma. I have applied a slightly modified version of the logic and it works perfectly well with the specific font I am using, eliminating the need for

font-family:"Courier New", Courier, monospace
.

$("#select-bill-period option").each(function(){
    var optionText = $(this).text();

    if(optionText.indexOf('+') > -1){ 
        optionText = optionText.split('+'); //if the string contains a + sign, split it into parts;

        //Handling the two-column case
        if(optionText.length == 2){
            var textLength = optionText[0].length - 1;

            for(var i=0; i<textLength; i++){
                optionText[0] = optionText[0]+' '; 
            }

            //Combine the text for the option
            $(this).text(optionText[0].replace(/ /g, '\u00a0')+''+optionText[1]);

        }else{
            var textLength = optionText[0].length - 4;

            for(var i=0; i < textLength; i++){
                optionText[0] = optionText[0]+' '; //Add space after the 1st column
                optionText[1] = optionText[1]+' '; //Add space after the 2nd column                   
            }

            //Merge the text for the option
            $(this).text(optionText[0].replace(/ /g, '\u00a0')+''+ optionText[1].replace(/ /g, '\u00a0')+optionText[2]);
        }

    }else{ 
        $(this).text(optionText); //Keep the text unchanged if there are no changes needed;
    }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide on triggering an open modal when clicking a link using jQuery

Creating a modal popup has always been my goal. After designing the CSS to style it, I found myself stuck as I lack knowledge in JQuery or JavaScript to code the functionality that animates and opens the modal upon clicking a button or link. Despite search ...

Implementing a JQuery script that triggers every time there is a change in the scroll

This script creates a shrinking effect on a box followed by a fade-in/out transition when the scroll position changes. However, the issue is that it triggers every time there is a scroll position change. For example, if the scroll position is at 100px and ...

Methods to disregard class prefix in css document

When I inspect elements in the Chrome console, I notice styles applied to certain divs/buttons like this: /* Sample Chrome browser styles */ .ItemClass1-0-3-171.ItemClass2-0-3-173: { background-color: "red" } I'm wondering how I can def ...

What are some tips for improving the smoothness and efficiency of my CSS @keyframes animation on SVG elements specifically in Firefox?

I noticed that this particular animation is causing high CPU usage on all browsers, although it runs more smoothly in Chromium. Is there a way to optimize its performance? Here's the SCSS code which is relatively simple: @keyframes laptop { from { ...

No animated scrolling occurring

I have been struggling with this problem for a while now and despite having gone through 100 questions, I still can't find a solution as to why my code snippet is failing. Here is the jQuery code I am using: $("#readmore").click(function(event){ ...

By having fadeIn responses triggered by livequery, we can avoid using automated ajax calls with setInterval for fading in

Hey guys, I've got a situation here. I'm working on implementing an automatic update feature (using ajax) on my website to show any new comments that have been posted since the page loaded. The ajax call is functioning properly and loading new co ...

Before the <table> tag, web browsers display a mysterious Phantom <br> tag

Why is the browser inserting a <br> tag before my <table> element? The page is meant to be displayed within one table, but an unwanted whitespace is being created by this tag that is not included in the code. The code appears like this, even w ...

What is the best approach to transforming my jQuery function into CSS to ensure responsiveness?

I have created a jQuery animation with four functions named ani1(), ani2(), ani3(), and ani4(). Everything is working fine on desktop, but now I am facing the challenge of making it responsive for mobile devices. I am looking for CSS code to replicate the ...

Utilizing JavaScript to Retrieve Selected Parameter in SSRS Report

Currently, I am incorporating an SSRS report into my website using Iframe. I aim to share a link to the specific filtered report, which is why I require knowledge of the report's parameters. My query pertains to how I can identify these parameters (e ...

Arrange the keys of a map in ascending order, prioritizing special characters and their precedence

JavaScript ES6 Map Example: const map = new Map(); map.set('first', ['1', '2']); map.set('second', ['abc', 'def']); map.set('_third', []); map.set(')(*', []); map.set('he ...

When should CSS compression and combining / JS minification be performed - at runtime or during build time?

I need to find a way to compress, version (for caching reasons), and possibly combine our JS and CSS files. I've come across two main approaches so far: 1) During the build process: Using an MSBuild task or similar. 2) Dynamically at runtime: Through ...

Utilizing Node.js and Express.js to send AJAX post requests

Having an issue with sending post data to http://localhost:1111/user/create. Utilizing the Expressjs framework and Redis as the database. The problem arises when the textbox value on the client side does not get captured, resulting in all field values bein ...

Creating a dynamic dropdown menu based on a class value using JavaScript in PHP

There are two dropdown menus on my webpage that display information from my SQL table. The first dropdown contains different "Colors," and the second dropdown contains members associated with each color group. Each member is categorized into different optg ...

Mastering the art of incorporating background image transitions and creating a seamless slider for navigation

Is there a way to create a navigation menu like the one on this theme? On this theme, when you hover over a menu item, the background image smoothly moves left or right to the item you are hovering on. If anyone knows of plugins that can achieve this eff ...

Having trouble debugging a website remotely? Dealing with a JSON problem specifically in IE8

Currently, the website is functioning flawlessly on various machines except for those used by the client in Africa. Unfortunately, I have no way of accessing his specific machine due to geographical constraints. The client has limited IT knowledge, and we ...

Switching to AngularJS for an upgrade

I am working with an AngularJS code and HTML. <input type="text" ng-model="selected" uib-typeahead="demos for demos in demo | filter:$viewValue | limitTo:8"> $scope.demo = ['demo-NV-enable','demo-NV-disable','demo-NV-shutdo ...

Dynamic div with height controlled by the content of the background image

I'm trying to figure out how to make a div with a background image of 100% width always maintain the aspect ratio of the image for its height. Is there a way to achieve this? In simpler terms, when I resize the page, I want the div to scale down whil ...

Using the spread operator to modify an array containing objects

I am facing a challenge with updating specific properties of an object within an array. I have an array of objects and I need to update only certain properties of a single object in that array. Here is the code snippet I tried: setRequiredFields(prevRequir ...

Contrast between the functionalities of $.ajax and $.post

$.post( url, data, callback, type ); $.ajax () Is there a distinction between using $.post and $.ajax in jQuery? And if so, which method is recommended? Also, how many parameters can be passed to the $.ajax function? ...

Using an AngularJS directive to trigger a function with ng-click

Everything is working well with my directive, but I would like to utilize it within ng-click. Unfortunately, the function inside the link is not getting triggered. Here's the link to the code snippet. <div ng-app="editer" ng-controller="myCtrl" ...