Setting the default selection in a closed dropdown menu with ng-Options

In my Angular project, I have a select dropdown that is populated from an array.

  • Each entry in the array is in lowercase format.
  • The variable "selected" that I use in other parts of my code needs to be in lowercase.
  • However, the entries displayed in the dropdown should be in uppercase format.
  • When the dropdown is closed, it should show the currently selected entry (in uppercase) with the prefix "VIEWING: "

While I know how to accomplish the first three points using

myValue.toUpperCase() for myValue in myArrayOfValues
, I am unsure on how to tackle the fourth point - either in Angular or with raw JS/jQuery.

Does anyone have a solution for this situation?

Answer №1

It seems like you're looking for a combination of the following:

Here is the HTML code snippet you can use:

<select name=dropdown size=1>
    <option value="option1">option 1</option>
    <option value="option2" selected>VIEWING: option 2</option>
</select>

This is the JavaScript code you can implement:

var dropdown = $('select[name="dropdown"]');

dropdown.focusin(function() {
    var selected = dropdown.find(":selected");
    selected.html(selected.val());
});

dropdown.change(function() {
    updateSelected();
    $(this).blur();
});

dropdown.focusout(function() {
    updateSelected();
});

function updateSelected() {
    var selected = dropdown.find(":selected");
    var html = "VIEWING: " + selected.val();
    selected.html(html);
}

You can also view a working example on https://jsfiddle.net/r89oy2zk/

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

matching equivalent URLs to identical states using an optional parameter in angular-ui-router

I am looking to map the following states to the same routes with a parameter: /en/live/ /en/ /en/live/football /en/football The goal is to store the 'live' part in a variable. I have attempted the following approach: $stateProvider.state(&ap ...

ERROR: Module 'jquery' not found in path 'C:....' when using Gulp and Browserify

Encountering an error: Error: Module 'jquery' not found in path 'F:...\newstyle\assets\lib\helper\html\img\js' at C:\Users...\AppData\Roaming\npm\node_modules&bs ...

The validation errors in the form of CodeIgniter are not being displayed by the JavaScript variable

Currently, I am utilizing the built-in validation_errors() function in CodeIgniter for my login form. The validation errors are displaying correctly in my view, but when I try to echo them into a JavaScript variable within the script to customize notificat ...

Issue with Laravel - JavaScript not functioning properly following the passage of a parameter through the route

I am currently working on a website that will display search results from various e-marketplaces. Everything was going smoothly with the JavaScript implementation until I attempted to pass parameters through the route. Once I did that, the results stopped ...

Leverage backend data in Angular-Google-Chart without the need for AJAX requests

I am attempting to transfer chart row data from a text field in HTML. For some unknown reason, I am unable to connect the model to controller SecondCtrl. The following code is a modified version of this http://plnkr.co/edit/3RJ2HS?p=preview from https:// ...

Using $http and JSESSIONID in Glassfish server

My backend is built with Java and it produces JSON for the services. The client needs to login and authenticate using cookies, specifically JSESSIONID in Java. I am able to receive the JSESSIONID from the server, but consecutive $http.get requests from the ...

Accordion menu causing Javascript linking problem

After following a tutorial, I managed to create an accordion menu in JavaScript. In the current setup, each main li with the class "ToggleSubmenu" acts as a category that can hide/show the sub-lis. Now, my query is this: How can I retain the link functio ...

Avoid production build warnings in Vue.js without the need to build the code

Experimenting with vuejs, I decided to use it for a simple page even though I could have achieved the same without any framework. Now, my project is nearly production ready. The only issue is that it's just a single js and html file, but it shows thi ...

Creating a three.js visualization of a cheerful X-Y coordinate graph

I am looking to generate a positive X-Y plane in mesh format using three.js. Additionally, I would like the ability to click on any intersection point and retrieve the coordinate values, resembling a graph paper layout. Design.prototype.mouseUp = functi ...

Javascript malfunctioning - exhausted all troubleshooting options

My JavaScript code consists of a single line, but I keep encountering the "getElementById null" error. document.getElementById("menu-background").style.left = "0"; HTML: <html> <head> <title></title> <link rel="style ...

What is the best way to switch the visibility of a div on click from another div?

My goal is to make a div toggle visible or hidden when another div is clicked. The only connection between the two divs is that they are both nested within the same parent div. There's a DIV element with the class of "comment" which contains a DIV ele ...

Firefox users may notice that the pop-up video player appears in unusual locations on their screen

I'm encountering an issue with a pop-up video player that is triggered by an "onClick" event in HTML elements. The problem arises when there are multiple video players on the same page - in Firefox, the first video loads at the bottom of the page, req ...

Restrict the option to select checkboxes

Can anyone help with limiting checkbox selection? This is the code I currently have... foreach($res as $res) echo '<div class="ediv"><input type="checkbox" class="echeck" name="pr[]" value="'.trim($res['product']).'" ...

Getting an error message like "npm ERR! code ENOTFOUND" when trying to install Angular CLI using the command "

Currently, I am eager to learn Angular and have already installed Node version 18.13.0. However, when attempting to install Angular CLI using the command npm install -g @angular/cli, I encountered an issue: npm ERR! code ENOTFOUND' 'npm ERR! sys ...

JavaScript: An object containing a unified handler for all method invocations

Let me explain further. Math.add(2, 2) //4 Math.multiply(4, 9) //36 Whenever a method in the Math object is invoked, it triggers a central processor that interprets the function name to determine its action. Can an object execute a default function when ...

Ajax successful event fails to trigger

Having Trouble Implementing Okta Authentication with WebForms The login functionality is working, but the redirect part is not functioning correctly I have attempted to use void and return a JSON object/string, but it does not seem to work If I remove th ...

Using jQuery to dynamically format a date with variables

Looking to properly format a date for use in a compare function to sort data $(xml).find("item").each(function () { var dateText = $(this).find("Date").text(); var year = dateText.substr(0,4); var month = dateText.subst ...

Efficient AJAX validation using PHP and JavaScript

I am currently working on a small website project that requires User registration functionality. One key aspect is ensuring that the system prevents users from registering with usernames that are already in use. Most modern websites employ AJAX to verify ...

Steps to establish the starting value as 'Concealed'

I stumbled upon this interesting example that demonstrates how to create expandable/collapsible text when clicked. My question is, how can I initially set the value to be hidden so that the paragraph starts off collapsed and expands only when clicked? He ...

The Angular ui-calendar introduces an innovative approach to event addition, providing users with

I need help with adding events to the angular ui calendar. Currently, I am using $scope.events.push() method to add events, but they get reset when changing the month. If anyone has experience with angular ui-calendar and event addition, please provide ...