Issue with populating dropdown menu inside jquery modal dialog box

When I click on the 'create new button', a modal window form pops up with a dropdown menu. The dropdown is supposed to be populated via ajax with the help of the populateUserData() function. Even though the ajax call seems to be successful, I am unable to see any data in the dropdown menu. Can someone please assist me in identifying what could be wrong? Thank you!

      <form id="formAddUser" action="#" title="Add User to Group" style="display: none;">       
     <label for="userId"><strong>User Id</strong></label>
      <select name="" id="userId" rel="0">
            <option>Select</option>              
     </select>  
     <label for="active"><strong>Active</strong></label>
     <select name="actvUsr" id="actvUsr" rel="1">
            <option value="Y">Yes</option>
            <option value="N">No</option>
    </select>  
   <button id="userGrpOk" >OK</button>
   <button id="usrGrpCancel">Cancel</button>
   </form>

    <button type="button" id="addusr">CREATE NEW</button>

Javascript

  var userDtl = $( "#formAddUser" ).dialog({
        autoOpen: false,
        width: 350,
        modal: true,
        });

       // open modal form.
       $( "#addusr" ).button().on( "click", function() {
               populateUserData();
               userDtl.dialog( "open" );
           });


 function populateUserData() { 
    var userDataXML = ajax data
    $.ajax({
        type : "POST",
        url : getUserdata',
        data : userDataXML,
        dataType : "json",
        contentType : "application/xml; charset=utf-8",
        async : false,
        success : function(jqXHR, textStatus,response) {
            var shtml='';
            $.each(jqXHR,function(index, data) {
                shtml+='<option value="'+data.name+'">'+data.name+'</option>';
                $("#userId").html("");
                $("#userId").html(shtml);

            });
        },
        error : function(jqXHR, textStatus, errorThrown) {
            alert("error");

        }
    });

}

Answer №1

If you are confident that:

  • you have properly entered the success callback function,
  • the data in jqXHR is accurate,

then consider implementing this within your $.each():

Need help adding options to a <select> with jQuery?

This will result in:

success: function(jqXHR, textStatus,response) {
    var $select = $("#userId");
    $.each(jqXHR, function(index, data) {
        $select.append('<option value="'+data.name+'">'+data.name+'</option>');
    });
}

UPDATE : Take note that the order of parameters should be maintained in the success function. According to the jQuery documentation :

success

Type: Function( Anything data, String textStatus, jqXHR jqXHR)

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

Tips for managing asynchronous file uploading in uploadify

I'm currently facing an issue with my uploadify plugin setup on my webpage. Below the plugin, there is a description field along with a textbox where users can input text. There is also a submit button positioned below these elements. My challenge lie ...

Sending a JSON object with scheduled task cron job

I have a PHP cron job that is quite complex. It retrieves data from an external webpage and consolidates all the information into one variable, encoding it in JSON. Unfortunately, this entire process is slow and consumes a significant amount of time. My d ...

Encountering a getMacChromiumEdgeDriverArchitecture error while using wdio/selenium-standalone-service

As I continue to learn WebDriverIO and adjust to MacOS, I encountered an error while trying to execute a spec file using "npm run test:wdio". The error message is as follows: > [email protected] test:wdio /Users/vinicius.correia/Desktop/dev/automat ...

Tips for creating multiple files using nodejs and express

I am currently working on developing a personalized code editor that consists of 3 textareas: html, css, and javascript. The objective is to save the data from each textarea into individual files. With the help of express and nodejs, I have successfully m ...

What is the best method to incorporate a JavaScript object key's value into CSS styling?

I am currently working on a project in React where I'm iterating over an array of objects and displaying each object in its own card on the screen. Each object in the array has a unique hex color property, and my goal is to dynamically set the font co ...

Easily integrating a JavaScript file into an HTML document when utilizing a NodeJS Express server

Currently in the process of developing a chat application, utilizing the Express server with NodeJS and AngularJS for client-side management. Encountering an issue when attempting to include /js/code.js in my html, as it cannot be found due to not being p ...

Having trouble retrieving the data property from the parent component within the child component slot

I am facing an issue with my Vue components. I have a rad-list component and a rad-card component. In the rad-list component, I have placed a slot element where I intend to place instances of rad-card. The rad-card component needs to receive objects from t ...

Elusive Essence: Mysterious Origins of the

Beginner in the world of Ionic and Angular. I am attempting to create a test app and incorporating the factory function. I obtained the design from Ionic Creator and now trying to add my code to it. Here is my controller file. angular.module('app.c ...

Creating NextJS Route with Dynamic Links for Main Page and Subpages

In my NextJS project, I have dynamic pages and dynamic subpages organized in the following folders/files structure: pages ├── [Formation] ├── index.js │ ├── [SubPage].js Within index.js (Formation Page), I create links like this: < ...

I am currently running a recursive loop to fetch data from MongoDB. However, the Express.js function runs through the entire script before the data can be successfully returned

Can someone assist me with setting up my Express route to handle the return of data from a recursive function that involves promises and fetching MongoDB data? Currently, my route is executing immediately without sending the data back to the client. The da ...

I encountered a TS error warning about a possible null value, despite already confirming that the value

In line 5 of the script, TypeScript raises an issue regarding the possibility of gameInstanceContext.gameInstance being null. Interestingly, this concern is not present in line 3. Given that I have verified its existence on line 1, it is perplexing as to w ...

Error: Authorization requires both data and salt arguments

As a novice in NodeJS, I attempted to create an authentication form using NodeJS + express. The issue I am facing is regarding password validation - specifically, when "confirmpassword" does not match "password", it should return nothing. Despite my effo ...

Firefox not responding to jQuery focus() function, but Chrome is executing it flawlessly

I have been searching for a solution to this issue for hours now, as I am aware that similar questions have been asked before. The problem at hand is that I have a table cell that displays a text gig of text. However, upon clicking the cell, the text shoul ...

Prevent input from being cleared when selecting an option with React-Select

Unfortunately, there was no satisfactory solution to this question so I will provide an answer: The issue arises when trying to utilize React-Select with a persistent input value that remains even after selection or blurring. Regrettably, this functionali ...

Having trouble with multiple Ajax forms in C# MVC not functioning properly

Currently, I am facing an issue with deleting dynamically generated rows in an HTML table using C# MVC. The problem is that I can only delete one row at a time and have to reload the page after each deletion for it to take effect. What could be causing thi ...

Modify background image upon hovering using AngularJS

I cannot seem to make the background images of my divs change. Despite trying various options, none of them have been successful. Here's an example of my code: <div ng-controller="mainController" class="main"> <div ng-repeat="land in lan ...

What is the reason why setting 'onClick' as an event handler is not flagged as a syntax error?

I am currently working on a JavaScript code snippet where I am trying to change the headline text when it is clicked. The code I have written is: var headline = document.getElementById("mainHeading"); headline.onClick = function() { headline.innerHTML ...

Obtaining Function Call Results by Querying Node.js MySQL Connection

One issue I'm encountering while trying to retrieve data from a database is the synchronization of calls. In my attempt to resolve this problem, I experimented with a demo example and used callback functions. Being relatively new to node.js, I am unce ...

AngularJS can retrieve the selected value from a select tag

<select ng-model="data.person"> <option value="1" selected="">1 pax</option> <option value="2">2 pax</option> </select> The ng-model above returned "1 pax," but how can I retrieve ...

Encountering difficulty in implementing two modals simultaneously on a single web page while utilizing the useDisclosure() hook in Ch

ChakraUI provides a custom hook called useDisclosure() which gives you access to isOpen, onClose , onOpen. const { isOpen, onOpen, onClose } = useDisclosure() You can use the onOpen function as an onClick handler for a button to open a modal. <Modal ...