Tips for showcasing the latter portion of text within an HTML select dropdown menu

I have a select drop-down list with a fixed width of 80px, as shown in the image below:

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

<select id="dd_country_code_2" name="dd_country_code_2" style="width: 120px;  height: 23px;">
    <option value="SEL">(Country code)</option>
    <option value="AF">Afghanistan ‏(‎+93)</option>
    <option value="AL">Albania ‏(‎+355)</option>
    <option value="DO">Dominican Republic ‏(‎+1)</option>
    <option value="FK">Falkland Islands ‏(‎+500)</option>
</select>

When a user selects an option, the result looks like this:

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

However, I want the second half of the text to be displayed within the 80px of the list so that the user can clearly see its country code.

For instance:

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

Could you please suggest a way to achieve this?

Answer №1

To change the text direction in CSS, you can apply the direction: rtl; property.

(function() {
  document.querySelector('select').addEventListener('change', function(e) {
    e.target.style.direction = 'ltr';
    if(e.target.selectedIndex > 0) {
      e.target.style.direction = 'rtl';
    };
  });
})();
#dd_country_code_2 {
  width: 120px;
}
<select id="dd_country_code_2" name="dd_country_code_2" style="height: 23px;">
    <option value="SEL">(Country code) </option>
    <option value="AF">Afghanistan (+93) </option>
    <option value="AL">Albania (+355) </option>
    <option value="DO">Dominican Republic (+1) </option>
    <option value="FK">Falkland Islands (+500)</option>
</select>

Answer №2

Dealing with the width constraint, here is an alternative approach that cuts off oversized list items.

An update includes using a pseudo-element to display a down arrow and allow for full customization of the input's dimensions

(function() {
  document.querySelector('select').addEventListener('change', function(e) {
    var val = e.target.options[ e.target.selectedIndex ].text.split('(');
    if (val[0].length > 12) {
      val[0] = val[0].slice(0, 10) + '...';
    }    
    e.target.nextElementSibling.value = val.join('(');
  });
})();
.selectable {
  position:relative;
  background-color:white;
  border:solid grey 1px;
  width:120px;
  height:18px;
  overflow: hidden;
  font-size: 12px;
}
.selectable:after {
  content: '\25BC';
  position: absolute;
  top: 0;
  right: 0;
  width:14px;
  height:18px;
  line-height:20px;
  pointer-events: none;
  font-size: 10px;
  text-align: center
}
.selectable select {
  position:absolute;
  top:0px;
  left:0px;
  font-size: inherit;
  border:none;
  width:120px;
  margin:0;
}
.selectable input {
  position:absolute;
  top:0px;
  left:0px;
  width:100%;
  height:100%;
  padding:1px;
  font-size: inherit;
  border:none;
  pointer-events: none;
}
.selectable select:focus, .selectable input:focus {
  outline:none;
}
<div class="selectable">
  <select>
    <option value="SEL">(Country code)</option>
    <option value="AF">Afghanistan (+93)</option>
    <option value="AL">Albania (+355)</option>
    <option value="DO">Dominican Republic (+1)</option>
    <option value="FK">Falkland Islands (+500)</option>
  </select>
  <input type="text" name="countrycode" value="(Country code)" />
</div>

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

Can you show me the way to open a single card?

Can someone assist me in making it so only one card opens when clicked, rather than all of them opening at once? Additionally, if there is already an open card and I click on another one, the currently open card should close automatically. If you have any ...

Transform your HTML audio player into a Vue component

I am in the process of converting an HTML player into a Vue component. Half of the component has been successfully converted, but the time control slider is still missing. Below is the original HTML code for the player: // JavaScript code for the audi ...

Dynamically loading a JSON file in JavaScript

Can you please review this code snippet? var scripts = {}; require = function(src){ var id = Math.round(+new Date()/1000); $.ajax({ url: src + '.json', type: 'GET', dataType: "json", cache: ...

How do I verify response values in Postman tests when the input parameter may be empty and can have multiple possible values?

In my program, there is an input parameter known as IsFruit that can have a value of either 0 or 1. If IsFruit is set to 0, the response should return fruits with a FruitsYN value of N. Similarly, if it is set to 1, FruitsYN should be Y. In cases where I ...

AngularJS controller encounters a scoping problem within a callback function

I have created an angular application with a simple login form that can be viewed on this JSFiddle. HTML Code: <form data-ng-app="jsApDemo" data-ng-controller="loginCtrl"> <label for="username">Username:</label> <input type=" ...

Managing empty props in React applications

I am facing an issue where I need to display an image fetched from an API on app start, but when I try to render it in React, the application crashes with the error message: TypeError: Cannot read property 'icon' of undefined. Although the icon ...

JavaScript automatically arranges child elements within their parent container in a random distribution without any overlapping

I am experimenting with creating a dynamic layout of circles (divs with border-radius) within a container without any overlap. Check out my progress here - https://jsbin.com/domogivuse/2/edit?html,css,js,output var sizes = [200, 120, 500, 80, 145]; var m ...

Tips for applying CSS conditionally to content at varying nesting levels

Currently, I am working with sass and finding it challenging to modify the structure of the page easily. The layout I have in place is due to my header having varying sizes depending on the specific page users are browsing: <div class="container"> ...

The Ajax GIF Loader is not functioning in the latest versions of Internet Explorer and Firefox, but it is running smoothly in

The animated GIF loader functions correctly in the latest version of Chrome, but does not animate when viewed in IE and Firefox. Below is the code snippet: $("#DIALOG").dialog({ buttons : { "Yes" : function() { ...

Experiencing difficulty receiving a full response from an ajax-php request displayed in a div

Having trouble getting a response in a div from an ajax request triggered by an onclick event in a form and a PHP page. Here's the code: <html> <head> <script language="javascript"> function commentRequest(counter) { new Ajax. ...

The scale configuration for scale: x is not valid for creating a time scale chart using chart.js

I am currently utilizing VueJS and attempting to integrate a time scale chart using Chart.js. However, I encountered the following error: Invalid scale configuration for scale: x Below is my configuration : First, I have a component named Chart.vue with ...

What is the best way to select a clicked span element within a div using JQuery and then adjust the spans before and after it

<div> <span>A</span> <span>B</span> <span>C</span> <span>D</span> </div> If a user clicks on one of the spans within the div, I want to apply styles (such as text color) to that span and all prec ...

Update the color of the div when all checkboxes in the hidden form are selected

Seeking help for multiple issues I'm facing with my code. Here is the link to the code. HTML for Upper Table: <div class="block"> <table> <tr> <th>Nr.</th> <th style="width: 200px">Task</th& ...

Looking to set up an event handler for the browser's back button in next.js?

When my modal opens, it adds a hash to the URL like example.com/#modal. I want to be able to recognize when the back button is clicked in the browser so I can toggle the state of the modal. The challenge is that since I am using next.js (server-side rend ...

Facing an issue in React-Native where unable to navigate to the second screen

I hope you don't mind the length of my query, as this happens to be my debut here and I am really eager to unravel the mysteries surrounding my code conundrum. Your assistance in this matter would be greatly appreciated. Could someone please lend me ...

Why is my CSS causing a div with 100% width to be slightly taller than expected?

I am working on a website that I want to adjust to the width of different browsers. Everything seems to be functioning properly, except for the fact that the parent div containing the 100% width divs always appears around 5 pixels too tall. This is causin ...

Print a table in PHP with a horizontal layout

I've been at this for hours and can't seem to figure it out. Hopefully someone out there can lend a hand. The code below is echoing the result in a table vertically, but I really need the cells to be next to each other horizontally. Any advice o ...

Is it possible to create a MongoDB query that can retrieve a specific number of documents from different types within a single collection?

If I have a collection named "pets" with three different types of animals: cat, dog, and bird What if there are 10 cats, 10 dogs, and 10 birds in the collection (30 documents total)? Can I create a query that retrieves 3 cats, 2 dogs, and 1 bird all at o ...

Activate night mode in ElementPlus using CDN

Is there a way to set the theme to dark in ElementUI + Vue when using CDNs instead of npm? <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width,initial-scale=1.0" /> ...

Creating Objects on the Fly with Mistic Query Builder

As someone who is relatively new to JavaScript, I have mainly focused on Java and PHP development. The JavaScript applications I have built in the past have been somewhat messy, difficult to test, and not easily extendable. Currently, I am working on crea ...