Having issues when dynamically adding options to a multiselect dropdown

I'm working on dynamically adding options to a multiselect using AJAX:

<select size='2' name="CraftCode" id=@ccrf class="form-control js-select manualentrydd" ></select>
$.ajax({
  type: "GET",
  url: "GetCraftCodes",
  data: {
    JobNum: jobnum.toString()
  },
  async: false,
  content: "application/json; charset=utf-8",
  dataType: "json",
  success: function(d) {
    for (var i = 0; i < d.length; i++) {
      $('.manualentrydd').append("<option value=\"" + d[i].craftCodeId + "\">" + d[i].craftCode + "</option>");
    }
  }
  $(".manualentrydd").multiselect("refresh");,
  error: function(xhr, textStatus, errorThrown) {
    return false;
    // TODO: Show error
  }
});

This code successfully adds options to my select element. I can see them in the inspect element, but the required list is not showing up and therefore my multiselect is not displaying any options. Here's a screenshot of the inspect element https://i.stack.imgur.com/nZrFt.png

Answer №1

If you want to include new options using the multiselect "addOption" function, you can do it as shown below:

$.ajax({
  type: "GET",
  url: "FetchCraftCodes",
  data: {
    JobNumber: jobnum.toString()
  },
  async: false,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(data) {
    for (var index = 0; index < data.length; index++) {      
        $('.manualentrydd').multiSelect('addOption', { value: data[index].craftCodeId, text: data[index].craftCode });
    }
  },
  error: function(xhr, textStatus, errorThrown) {
    return false;
    // TODO: Display an error message
  }
});

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

Is there a way to retrieve the modal's viewport height in Angular?

Is it possible to determine the viewport height of my ng bootstrap modal within my Angular application? Here is what I currently have: I have a modal with CSS styling as shown below: .modal-xxl { width: 95% !important; max-height: 90% !important; ...

When utilizing jQuery to add a <li> element, it suddenly vanishes

? http://jsfiddle.net/AGinther/Ysq4a/ I'm encountering an issue where, upon submitting input, a list item should be created with the content from the text field. Strangely, it briefly appears on my website but not on the fiddle, and no text is appen ...

Ways to create distance between repeated cards in a loop. My method involves utilizing ajax, jquery, and bootstrap

view image description here What is the best way to create some space between these closely aligned cards? .done((todos) => { todos.forEach(el => { console.log(el) $("#todo-list").append(` <d ...

Manipulating child classes using jQuery

I am struggling to display an X icon next to a tab on my page when the tab is clicked, but I am facing difficulties in toggling its visibility. The issue arises when trying to access the span element within the tabs. $('.tabs .tab-links a').on(& ...

What could be causing my popup window to not display on top of all other HTML elements?

.box { width: 40%; margin: 0 auto; padding: 35px; border-radius: 20px/50px; background-clip: padding-box; text-align: center; display: inline-block; } .button { font-size: 1em; padding: 10px; color: #222; border: 2px solid #06D85F; ...

Using CakePHP4: Transmit Data via Ajax from Controller to View

I'm trying to retrieve data from the database and send it using Ajax in CakePhp4 from the controller to the view. Although I've managed to implement it (with sparse documentation), I'm facing an issue where it doesn't return me the arr ...

The for loop is throwing an error because the variable 'i' is not defined

I recently started using eslint and I'm in the process of updating my code to comply with my eslint configuration. module.exports = { env: { browser: true, commonjs: true, node: true, es2021: true, }, extends: 'eslint:recomm ...

Discover the method for displaying a user's "last seen at" timestamp by utilizing the seconds provided by the server

I'm looking to implement a feature that displays when a user was last seen online, similar to how WhatsApp does it. I am using XMPP and Angular for this project. After making an XMPP request, I received the user's last seen time in seconds. Now, ...

How to smoothly glide to the bottom of a chat box by scrolling synchronously

I am currently in the process of developing a chat application. Each time a user sends a new message, it is added to a list of messages displayed in an unordered list (ul). I have successfully made the ul scrollable, but unfortunately, when a new message i ...

Amplify encounters the "Failed to load resource: the server responded with a status of 400" issue

I encountered an error while using Amplify, although the build was completed successfully. Failed to load resource: the server responded with a status of 400 manifest.json:1 The system is functional in the local environment. Below is the Package.json scri ...

Is Meteor.js the solution for time-triggered server requests?

We are currently in the process of developing an application that matches users from a database every Wednesday and Friday. How can we achieve this using Meteor? In the server code, I am considering organizing this functionality within a timedserver.js fi ...

Utilize Ajax to execute a task based on the response received

Using jQuery Ajax, I am sending an id to another page where I select a column from a MySQL database and echo a row based on that id. While this part works well, the second function is not working. My question is, how can I make a click event happen on the ...

Creating a Form with a Custom Format in AngularJS

Just starting out with AngularJS and currently working with ACTIVITI. I'm looking to create a form in a specific structure where each response follows this format: { "taskId" : "5", "properties" : [ { "id" : "room", ...

I am struggling to increase the width of my input bar and center-align it

Below is the CSS class I've created to ensure that all elements remain centered on the user's screen. container: { display: "flex", position: "absolute", top: 0, left: 0, height: "100%&qu ...

I am wondering if there is a way to utilize jquery tsort in order to sort an unordered list while having only the ul as

Typically, when utilizing tsort to sort a ul, the syntax is as follows: $('ul>li').tsort(); In my case, I have access to the ul through a variable created like this: var myul=$('#mydiv').find('ul'); My question is, how ...

Issue encountered while generating a package using npm init in Node.js

I am currently in the learning process of NodeJs from tutorialspoint(TP). Following instructions provided in this link, I tried to create a package by running the following command: C:\Program Files (x86)\nodejs>npm init This utility will w ...

A comprehensive guide on associating a JavaScript function with an element attribute

I am looking for a way to assign a JavaScript function to an HTML attribute. For example: <li data-ng-repeat="job in jobList" class= dynamicClass(str) data-filter = "dynamicFilter(str)"> The reason I want to do this is because the class name and ...

Having trouble accessing listview capabilities in jquerymobile?

I am currently delving into the world of jquerymobile. I am experimenting with a sample project where I aim to showcase a listview. Below is the code that I have been working on. <!doctype html> <html> <head> <link rel="stylesheet" h ...

What is the reason for the find() method not displaying the most recent data from a MongoDB database in an Express.js application?

Upon calling the app.post('/form-submit', funtion(req, res)) method, my expectation is for it to first save the data using save(). This works fine, but then when I call the find() method, it shows all the data from the mongoDB database except for ...

Can Selenium in JavaScript be used to retrieve child elements from a webpage?

I've been struggling to adapt my JavaScript script for use with Selenium (also in JavaScript). Despite numerous attempts, I haven't been able to find a solution. I've attached an image to better explain my question await chromeDriver.findEle ...