Displaying a dropdown selection that showcases two object properties lined up side by side

I need the select option dropdown values to display employee names along with their titles in a lined up format. For instance, if my values are:

Bob Smith Director  
Mike Kawazki HR
Jane Doe Manager

I want them to be shown as:

Bob Smith      Director  
Mike Kawazki   HR
Jane Doe       Manager

The solution I currently have is similar to the one provided in this post. However, that solution relies on font styling to align the text correctly, and I need to maintain the specific font I am already using.

Answer №1

This example is compatible with the specified font:

var spacesToAdd = 5;
var maxcharacters = 0;
var toadd = ""; //ensure it remains empty!! Alternatively, you can use a hyphen (-)
const array = [];
function addv(num){
array[array.length] = num;
}
$("#timezones option").each(function(){
var parts = $(this).text().split('|');
var len = parts[0].length;
addv(len);
});
var largest = Math.max.apply(Math, array);
$("#timezones option").each(function(){
    var parts = $(this).text().split('|');
    var strLength = parts[0].length;
    var strLength2 = parts[1].length;
    var larg = largest + spacesToAdd;
    var numm2;
    if(strLength == largest){
    numm2 = spacesToAdd;
    }else if(strLength < largest){
    numm2 = larg - strLength;
    }
    if(toadd == ''){
    toadd = '\u{A0}';
    }
    for(var x=0; x<(numm2); x++){
        parts[0] = parts[0]+'⇴'; 
    }
    $(this).text(parts[0].replaceAll("⇴", toadd)+parts[1]).text;
});
#timezones{
    font-family:"Lora", monospace
}
option {
  background-color: #ffffff;
  font-family:"Courier New", Courier, monospace
}
option .l{
float: left;
}

option:before {
  content: ">";
  font-size: 20px;
  display: none;
  padding-right: 10px;
  padding-left: 5px;
  color: #fff;
}

option:hover:before {
  display: inline;
}
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lora">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select name="timezones" id="timezones">
    <option value="1">Bob Smith | Director</option>
    <option value="2">Mike Kawazki | HR</option>
    <option value="3">Jane Doe | Manager</option>
</select>

Do you think this solution will meet your needs? Simply remove the Lora font and opt for the alternative font.

Adjust toadd to "-" if desired! Remember to include spaces around your | for proper formatting from left to right.

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

The AJAX query for validating IDs in a JSP page consistently produces identical outcomes

I have been implementing a feature to check IDs using AJAX in JSP. When a user tries to create a new account, they enter the desired ID and AJAX checks if the ID already exists in the database. If the ID is found, it returns 'n' which is then dis ...

CSS division nested within the center float division

In my attempt to design a 3-column home layout, I have successfully styled the center, left, and right sections. However, I am facing difficulty in placing two sliders within the middle class. My goal is to have slider1 positioned on top inside the middle ...

How to easily center text across multiple lines using CSS

Looking for a way to center text horizontally and vertically within a div? I had it all working perfectly with this code snippet from a Stack Overflow post: text-align: center; vertical-align: middle; line-height: 90px; /* set line height to match you ...

Tips for Storing Your Data Collection: A Guide on Different Storage Options

In the different parts of my app, I require access to a dataset containing timezones. Traditionally, I have stored such data in a class method like Timezone.all_zones (Ruby) for easy accessibility anywhere in my code. Is there a way to achieve this same ...

The React data editor Dialog closes when the drop-down is clicked without triggering any onChange events

Utilizing the react-datasheet component, I have implemented a table to display a matrix/grid of data. To enhance user experience, I customized the dataEditor to launch a custom dialog where users can only choose from preselected values in a material-ui dro ...

Design a dynamic top navigation bar in Angular or any other framework that automatically adjusts to the size of the screen, similar to the responsive bookmarks bar in

Could you offer guidance or suggestions on how to design a navigation bar (using Angular 1, jQuery, CSS, etc) that emulates the functionality of Google Chrome bookmarks bar when resizing the page? Essentially, as the page size decreases, a new button/symbo ...

How can I filter rows in HTML using Vue.js based on string options?

When selecting different options, I want to dynamically add new rows. For instance, if "kfc" is selected, a specific row should be added. If "cemrt" is chosen, another row needs to be included. <div class="card-content" v-for="(bok, index) in rules" :k ...

Ways to retrieve several parameters from a controller using Ajax Jquery in Codeigniter

I need to retrieve a list of images from a folder based on a specific ID. Currently, I am able to get the file names but I also require the upload path. Is there a way to obtain both sets of data using a single function? Javascript Code: listFilesOnServ ...

I am currently attempting to extract data from a JSON file by using key names for reference, but I am running into issues when dealing with nested keys

Is there a better way to retrieve values from a JSON file by matching key names? The current method I am using does not seem to work with nested keys, so any suggestions on alternative approaches would be appreciated. // Sample .JSON file { "ro ...

What is the best way to add a gradient effect to my image? (link)

All the details are provided in this JSFiddle. Apologies, I am unable to display the link here. It can be found in the comments section! I'm facing difficulty achieving a red gradient over the image.. ...

Is there a way to display a message in a div container instead of using the alert box when there is a successful ajax response?

Hey there, I'm currently working on implementing error validation handling for a custom form that I've created. I'm looking to display the error messages in a designated div rather than using the standard browser alert box. Since I'm fa ...

Sharing an object's attributes as props in Vuejs

Greetings everyone, I am facing some confusion. I am working with two components (child and parent component) where I pass the properties of an object as props <child :child-data="abc" ></child> Vue.componen ...

Videos embedded using the React HTML video tag are not automatically playing on mobile devices

I have implemented a jsx variable to insert a video into my html. Despite following the advice to include muted defaultMuted, and playsinline (which I have already done), the videos autoplay on safari, chrome, and firefox on my computer but not on mobile ...

"Utilize the on() method to bind a click event to dynamically generated elements received

After reading several discussions on using on() to link events to dynamically generated HTML elements, I decided to create an example (FIDDLE) where the click event is bound to elements div.clicktitle fetched via AJAX. These div elements contain data attri ...

I recently discovered how to modify the video source (src) within an html5 source tag, but I am encountering varied outcomes when using it within a function or with inline javascript

When attempting to change the src of a video tag using Javascript, I encountered an issue. The code works fine when placed inline, but as soon as I try to include it within a function or in the "onclick" event of a button tag, nothing happens. There are no ...

Modify certain user configurations without altering all settings in one specific form using PHP and MySQL

I am in the process of developing a PHP website with data stored in a MySQL database. Currently, I have completed the sign-up, login, and logout functionalities. However, I am looking to enhance user experience by adding a section where users can adjust th ...

Ensure the text value of a collection of web elements by utilizing nightwatch.js

Recently, I started using nightwatch.js and I am trying to retrieve a list of elements to verify the text value of each element against a specific string. Here's what I have attempted: function iterateElements(elems) { elems.value.forEach(funct ...

Canvg | Is there a way to customize canvas elements while converting from SVG?

Recently, I encountered an issue with styling SVG graphics dynamically generated from data. The SVG graphic appears like this: https://i.sstatic.net/xvIpE.png To address the problem, I turned to Canvg in hopes of converting the SVG into an image or PDF us ...

Selenium Webdriver faced challenges in identifying dynamically created scripts using CSS selectors

I am in need of assistance with the following: Requirement: On the homepage, there is a navigation menu bar that changes appearance when scrolling down. This change is reflected in the body class name switching from "home" to "home scriptable persistent-o ...

What is the process for loading a font file in Vue.js and webpack?

I've done a lot of research, but I couldn't find any links that show me exactly how to add fonts in VueJS. This is the method I'm using to import the font in my LESS file: @font-face { font-family: "Questrial"; src: url("../../fonts/Que ...