Refresh the dropdown menu selection to the default option

My HTML dropdown menu includes a default option along with dynamically generated options using ng-options. The variable dropdown is bound to the dynamic options, not the default one.

<td>
    <select ng-model="dropdown" ng-options="item as item.name for item in items">
        <option value="">--- Select an item---</option>  <!-- default -->
    </select>                                   
</td>

This code results in the following HTML:

<td>
    <select ng-model="dropdown" ng-options="item as item.name for item in items">
        <option value="">--- Select an item---</option> 
        <option value="0" label="item1">item1</option>
        <option value="1" label="item2">item2</option>
        <option value="2" label="item3">item3</option>
    </select>                                   
</td>

I am looking to add a reset button that will return the dropdown to the default "Select an item" option. However, since this option is not linked to the model, I cannot use something like:

$scope.dropdown = items[0] // this would select item1

or even

$scope.dropdown = null

How can I implement a reset functionality for my dropdown to go back to the first option?

Answer №1

Here is an example code snippet to achieve the desired functionality.

var app = angular.module("app",[]);

app.controller("MyCtrl" , function($scope){
   $scope.items = [{name:"john"},{name:"jane"}];
  
  $scope.dropdown = $scope.items[0];
  $scope.clearSelection = function(){
    $scope.dropdown = {};
    }
  
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="MyCtrl">
   
  <select ng-model="dropdown" ng-options="item as item.name for item in items">
        <option value="">--- Select an item---</option> 
       
    </select>  
  <button type="submit" name="button" ng-click="clearSelection()">Clear Selection</button>
  
</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

Step-by-step guide on serving index.html for angularjs and other frameworks using a server

After scouring Stack Overflow and various forums without success, I finally stumbled upon a solution that worked for me. I wanted to share it here in case it helps someone else :) This solution is specifically tailored for the folder structure outlined be ...

What is the best way to display content next to an image, as well as below the image once it finishes?

Currently, I am working on customizing my blog posts in WordPress. My goal is to have the content of each post displayed next to the post thumbnail, and once the image ends, the content should seamlessly flow underneath it from the left side. I have imple ...

Trouble with Ajax program loading properly in Chrome browser

I have developed a small ajax program and encountered an issue. While it works perfectly fine on Internet Explorer 11, it does not function correctly on Chrome and Firefox. Here is the HTML file snippet: <html> <head><title>Ajax Page< ...

Inquiries about ngshow and the scope concept

I have a question about using AngularJS. I have multiple sections and only want to display one at a time using <section ng-show="section6_us"> </section> and <section ng-show="section7_us"> </section>. My scope has many variables. ...

Tips on Guaranteeing AJAX Requests are Successfully Called in Sequential Order and Receive Responses in the Same Sequence

What is the best way to guarantee that AJAX requests are executed in a specific order and receive responses in the same order? ...

Is there a way to reset an AngularJS controller to its initial state after a data change?

I have been trying to understand the API documentation for this, but it seems a bit challenging. Let's say I have a controller that retrieves data on its initial call: myCtrl = function ($scope, Data) { $scope.data = []; data_promise = Data.getD ...

A guide to update values in mongodb using node.js

I'm working on tracking the number of visitors to a website. To do this, I've set up a collection in my database using Mongoose with a default count value of 0. const mongoose = require('mongoose'); const Schema = mongoose. ...

Formik: encountering target as undefined while passing Material UI Date Picker as prop to React Component

I'm currently working on developing a component that can be reused. The idea is to pass form fields as props, but I ran into an issue when clicking on the datepicker field. The error message that pops up is: TypeError: target is undefined Any sugge ...

Is there a method available to minimize the size of a local storage list containing strings?

Hey there, I am trying to load a large 2.5MB json file in my browser so that I can use it for some typeAhead functions. Unfortunately, I'm facing an issue with my local storage being constantly full. When using Firefox, I receive the following error ...

Managing the clearing of a view component in React or Vue.js

In my project, I have a container component that handles all the business logic, ajax requests, and data management. This container component has child components to which it passes data and methods as props. For example, there is a "CommentForm" compone ...

The process of combining identical data values within an array of objects

Can anyone help me with merging arrays in JavaScript? Here is an example array: [{ "team": team1, "groupname": "group1", "emp-data": [{ "id": 1, "name": "name1", }], }, { "team": team1, "groupname": "group1", " ...

A guide on removing a database row using Angular and PHP

I am currently working on a project that involves the combination of Angular and PHP. One issue I have encountered is when trying to delete a row from a list of customers displayed on an HTML page. Despite clicking the "delete" button, no deletion occurs, ...

AngularJS in Action: Communicating Between Services, Controllers, and Directives

There is a situation where some menu items are enabled while others are disabled in the user interface. When a user clicks on a disabled menu item, an error is displayed on the page. To address this issue, I attempted to use AngularJS service and $broadca ...

What is the best way to ensure that custom JavaScript and CSS files in Sphinx are always loaded with the most recent changes

Within the configuration file conf.py for Sphinx, I have specified the following: html_css_files = ['css/custom.css'] html_js_files = ['js/custom.js'] However, any alterations made to custom.js or custom.css do not immediately appear i ...

AngularJS directive ngShow not working properly

It seems like I'm encountering some scope issues that I can't seem to resolve. Here's a rundown of the problem: I'm trying to create a custom directive called "my-create-content" that will insert an "img" HTML template into the element ...

Is it possible to dynamically load a specific div when the page loads, relying on the

I am using JQuery SlideUp and slideDown methods to toggle the visibility of panels. How can I load or display the first record's contact information as a default? Currently, it loads blank because I set the panel's display property to none: < ...

The value of the bound variable in ng-options does not get updated after the array is

I have a situation where I am populating a list using an array and the ng-options directive in AngularJS. The selected item is bound to a variable called myObject.selectedItem. However, I noticed that even after clearing the array, the value of myObject.se ...

What is the best way to vertically align an InputLabel within a MUI Grid item?

I'm trying to vertically center the InputLabel inside a MUI Grid item. Here's what I've attempted: import { FormControl, Grid, Input, InputLabel, TextField } from "@mui/material"; export default function App() ...

Error in electron-builder: Module 'dmg-license' was not found

Seeking a straightforward method to create an electron app for macOS from a Linux machine. Unfortunately, the electron-builder -m command is not functioning properly. Here is the complete output of the command: electron-builder -m • elec ...

Tips for ensuring the vertical scroll bar is always visible on a div element

I need help with maintaining a vertical scroll bar on a div regardless of its height. .myclass { overflow-y: scroll; } <div class="myclass"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the in ...