Is there a way to fill in the identical value in the dropdown list below?

click here for the first image

click here for the second image

In my controller, I have two viewbags set up like this:

ViewBag.MyCategory = new SelectList(db.RecyclableType, "id", "type");
ViewBag.iddd = new SelectList(db.RecyclableType, "id", "type");

And in my View:

@Html.DropDownList("iddd", (IEnumerable<SelectListItem>)ViewBag.iddd, "SELECT ITEM TYPE", new { @class = "form-control" })
@Html.DropDownList("MyCategory", (IEnumerable<SelectListItem>)ViewBag.MyCategory, "SELECT ITEM TYPE", new { @class = "form-control"})

I attempted to create a function as shown below but it is not functioning correctly:

document.getElementById("iddd") = document.getElementById("MyCategory");

Answer №1

If you want to accomplish this task, jQuery is the way to go.

  1. To start, listen for changes in the iddd dropdown and retrieve its value
  2. Next, assign this value to the MyCategory dropdown and mark it as selected
$("#iddd").change(function () {
    var value = $("#iddd").val();
    $("#MyCategory").val(value).attr("selected", "selected");
});

Important: Remember to include the jQuery script file in the head section of your document

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

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

Using React to create a fadeout effect and decrease the length of the photo

Is there a way to create a fade-out effect for each photo card and then shorten the length of the photos following the fade out? I have tried implementing a solution, but it doesn't seem to work when I click on each photo. Any suggestions or insights ...

Spinal cord: Connecting to the surrounding container of the view

Within my Backbone IndexView, I am using a TaskView for each 'task' model. I would like to bind an event to the li element that encloses the taskview. For instance, if the 'className' attribute is 'task', I want to trigger an ...

Tips on incorporating redirect link to Material UI icon's onclick in React

I'm encountering some issues when trying to add a function to the VideocamOutlinedIcon on line 232. I want this function to include an onclick event that redirects the user to an external domain link in a new tab. Despite attempting various solutions, ...

Using Variables in ASP.NET Web Forms Pages

Struggling to access a variable or property in my .cs file from the .aspx file. Despite trying various solutions found in different threads, I'm still unable to get it to work. Here's the situation: A GridView control with textboxes and a Require ...

Discovering the RGB color of a pixel while hovering the mouse in MapboxGL Js

Looking to retrieve the RGB color value of the pixel under the mouse hover in MapboxGL Js. What is the most effective method to accomplish this task? ...

Is there a way to access the body html element within a template in vue.js?

I'm struggling to add styles to the body element of my HTML page. It seems like I can't access it directly from the template because there's another body element nested inside the main body. Even when I try to change the style using JavaScri ...

React: Modifying values of individual input fields

Utilizing React, I have developed a basic list that showcases data fetched from an API. The fetching and updating of data is done through axios. An issue arises when updating a specific element in the list, causing all inputs to clear except for the acti ...

Exploring the depths of Javascript with recursive AJAX callbacks and tackling the challenge of reaching

I'm facing a dilemma in understanding why my code is running perfectly. Below is the snippet of my code: function updateForNextLecturer(current_id, all_lecturers, progressbar) { $.getJSON( '/' + root + 'custom/ajax/update_ ...

Enhancing AngularJS routing with dynamic partial parameters

I have experience working with routes in different frameworks like Rails and Laravel, but I am now facing a challenge while working on an AngularJS site. In Laravel, we could dynamically create routes using foreach loops to handle different city routes. Ea ...

Creating Various Views of the Same 3D Model

After creating a 3D model from Mixamo and converting it into a component using npx gltfjsx, I am facing an issue when trying to render this model multiple times on Canvas. Despite my attempts, only one instance of the model appears. Is there a solution to ...

Encountering issues with pipes and ngModel functionality in Angular causing errors

I have a unique custom dropdown feature that binds the selected dropdown value to an input field in my reactive form using ngModel. I have also implemented a mask on the input fields using a pipe. Here is all the relevant code for this functionality: HTML ...

Expanding and collapsing multiple div elements easily with Javascript code

I am exploring how to customize the expand/collapse functionality for multiple DIVs on my webpage. Currently, I have implemented a code snippet that toggles an entire class, but I am wondering if there is a way to target the specific element being clicked ...

Comparison of Performance between Linq to SQL, Entity Framework, and NHibernate in an ASP.NET 3.5 Mashup-Style Application

I have been assigned the project of developing a web 2.0 style mashup application that heavily relies on integrating various third-party webservices such as YouTube and Twitter. In addition, there will be several custom features implemented using ASP.NET ...

Tips on reversing a numeric value with scientific notation in nodeJS

Exploring the optimal method to reverse an integer (positive and negative) in NodeJS 12 without the need to convert the number to a string. This solution should also accommodate numbers written in scientific notation such as 1e+10, which represents 10000 ...

How can JavaScript be used to dynamically display submitted HTML form values on the same page, as well as how to perform calculations on the form data?

Currently, I am working on creating a form for collecting passenger information at an airport. The form includes fields for first name, last name, passenger weight, and cargo weight. Upon submitting the form, I aim to display the entered information along ...

Best Practices for Creating Custom 404 Pages in ASP.NET Web Forms 4.5

After spending a week scouring the internet, trying various solutions, and testing different methods, I have unfortunately not been able to achieve the satisfactory outcome I was hoping for. Issue Summary: Working with IIS 7.5 The problem at hand is th ...

Is it necessary to bump the major version if I make updates to a react module that does not affect existing code functionality, but may cause Jest snapshot tests to break?

Imagine I am developing a module for a react component and currently working on a PR to introduce a new feature. Along with this new feature, I have also made changes to the component by refactoring it to eliminate certain internal parts that were previou ...

picking out a particular set of data from a JSON document

I have a map of Europe along with a JSON file that displays the unemployment rate for each country in the year 2011. The JSON file also includes x and y elements, allowing me to place a blue circle on top of each country on the map. My goal is to be able ...

Issue with JSON-to-MUI card mapping causing absence of UI components or error messages

I'm facing a challenge with mapping this JSON data to MUI Cards. Despite no error messages, the content isn't being displayed as expected. Surprisingly, when I make changes unrelated to the issue, the console.log(questionGroups) only shows the JS ...

Custom properties of an object are being erased when converting to JSON format within the canvas

I am working on a canvas project that involves multiple image objects, each with custom attributes. My goal is to save this canvas as a json object in a database, but the conversion process seems to strip away the custom attributes. Currently, I am using t ...