How can I extract the value from the object returned by an AJAX call?

HTML file

<div class="container">  
  <table id="headerTable" class="table table-bordered">
   <thead>
     <tr>
      <th colspan="2">Header</th>
     </tr>
  </thead>
  <tbody>
    <c:forEach items="${headerList}" var="field">
        <tr>
            <th>${field}</th>
            <td><input id="${field}" type="text" class="form-control "></td>
        </tr>
    </c:forEach>    
  </tbody>
</table>

Javascript

$('#convertBtn').click(function() {
  var convertMsg = $('#msgText').val();
  alert("conversion message is " + convertMsg);
  $.ajax({
    type: "GET",
    url: "/convertMessage",
    data: {
      "msg": convertMsg
    },
    success: function(data) {
      //data format looks like Object {SubsystemChannel: "F", MessageNumber: "200257", DebugQueue: " ", WorkStationNumber: "023", FrontEndNumber: "0000"…}

      $('#headerTable input').each(function() {
        var id = $(this).attr('id');
        var field = data.id;
        $(this).val(field);
      });
    }
  });
});

My goal is to loop through the $('#headerTable input'), set the value(from the data) for each input. However, I am facing an issue with this logic. Can anyone provide assistance on this matter? Thank you in advance.

Answer №1

It is recommended to utilize bracket notation rather than dot notation when accessing properties with the variable id.

$('#headerTable input').each(function () {
    var field = data[$(this).attr('id')];
    $(this).val(field);
});

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

Guidelines on navigating a blank page using the Nuxt-js method

I have run into an issue in my Nuxt.js project where I need to open a link in a new target when a user clicks a button. Despite trying various solutions, I haven't been able to find a workaround within the Nuxt.js framework itself. <a :href=&qu ...

Transfer the index of a for loop to another function

Can you explain how to pass the 'i' value of a for loop to a different function? I want to create a click function that changes the left position of a <ul> element. Each click should use values stored in an array based on their index posi ...

A guide on transforming HTML into a variable using JavaScript

Having trouble converting HTML to a JavaScript variable. This is my HTML code: <div onclick="openfullanswer('2','Discription part','This is the code part');"> I want to create this HTML code dynamically, but I&apo ...

Creating a Node.JS application, developing an Express JS API, and handling errors

I am facing challenges while developing a REST API in Express.js and I am seeking assistance. Below is my Express.js code: router.get( '/apps/download/:downloadId', function ( req, res, next ) { const opts = Object.assign( {downloadId: re ...

Tips for utilizing the onload attribute alongside the ng-controller directive to run a function that has been established within the controller

When trying to call the submit() function as soon as the page loads, I keep encountering a submit() method not found error. The positioning of ng-controller and onload is confusing to me. If there is an alternate method in Angular to achieve this, please ...

Troubleshooting the Issue of Selecting a Menu Item with Jquery and AJAX

How can I trigger the AJAX Javascript function (Code-1) when selecting or clicking on the "Log Report" menu item (Code-2) and display the content of test.txt in an HTML text box on the page? I have a JavaScript function code-1 that reads the contents of t ...

Ways to incorporate a custom JavaScript function that is activated by an external server system?

I'm currently exploring a JavaScript widget that needs to operate within specific constraints: The widget initiates a request to a third-party server using a callback URL The third-party server pings the callback URL after a set period, triggering a ...

Having trouble selecting a radio button in React JS because it's marked as read-only due to the check attribute

In my scenario, I have a child component with radio buttons. The questions and radio buttons are populated based on the data, with each set consisting of one "yes" and one "no" option. I am attempting to automatically check all radio buttons that have a v ...

How can I confirm if a class is an instance of a function-defined class?

I have been attempting to export a class that is defined within a function. In my attempts, I decided to declare the class export in the following way: export declare class GameCameraComponent extends GameObject { isMainCamera: boolean; } export abstra ...

Looking for jQuery bbq... is the grill fired up yet?

In my exploration of the jQuery bbq plugin, I noticed that there is no reference to document.hash in the code. I believe that the function for retrieving the hash is located at line 1094: function get_fragment( url ) { url = url || location.href; ...

Animating content through CSS and jQuery to reveal its unfolding effect

Just stumbled upon the amazing quote-expansion animation in the OSX Mail app and I am completely impressed. I am on a mission to bring that same magic to the web (or at least close to it), but unsure if anyone has done something similar yet. A couple of ...

Developing a RESTful API with Discord.js integrated into Express

Currently, I am faced with the challenge of integrating the Discord.js library into an Express RESTful API. The main issue at hand is how to effectively share the client instance between multiple controllers. The asynchronous initialization process complic ...

Linking numerous promises generated by a for loop

After delving into the concept of promises through this resource, I grasped the fundamental idea behind it. var parentID; $http.get('/api/user/name') .then(function(response) { parentID = response.data['ID']; for (var i = 0; i ...

Storing checkbox input in a database using Django and refreshing the page with the updated information

Dealing with a list of 'Assignment' objects each having a 'status' field, which can be true or false, and users can toggle this value by clicking on a checkbox attached to a specific assignment. Here is a visual representation of the f ...

Is there a way to verify that all of my ajax requests have finished?

I am facing a situation where I have three <select> inputs on my form, each with an option that has a zero value. If the selected value is zero, then I do not need to trigger an ajax request. However, if the option does have a value other than zero, ...

What is the best way to incorporate this CodePen snippet into a Vue project?

Can anyone help me figure out how to incorporate this awesome animation from CodePen (link here: https://codepen.io/iprodev/pen/azpWBr) into a Vue project? I've tried implementing it like so: <template> <div> <canvas heigh ...

Promise<IDropdownOption[]> converted to <IDropdownOption[]>

I wrote a function to retrieve field values from my SPFx list: async getFieldOptions(){ const optionDrop: IDropdownOption[]= []; const variable: IEleccion = await sp.web.lists.getByTitle("Groups").fields.getByTitle("Sector").get ...

What is the best way to capture user input using an onClick event in JavaScript and then display it on the screen?

I'm in the process of upgrading a table, and while most of it is working fine, there is one function that's giving me trouble. I want this function to return an input with an inline onClick event. The actual return value is displaying correctly, ...

Prevent the creation of references to objects passed as function parameters in a separate list

I'm facing an issue with modifying items from an array and adding them to a new list of objects. The problem arises when I make changes to the item in the new list, it also reflects those changes in the original array. It seems like there is some ref ...

Implementing a jQuery AJAX form that handles file uploads

I am having trouble finding a solution for this issue, so I need some assistance... Form: <div class="status alert alert-success" style="display: none"></div> <form id="main-contact-form" class="contact-form" name="contact-form" method="po ...