Expanding Javascript Array Objects with Dynamic Parameters based on Conditions

I'm trying to dynamically add a parameter to the array object url based on a function's value.

Unfortunately, my attempts so far have been unsuccessful.

var change_lookup_number = function() {
  var type = document.getElementById('type_number').value;
  if (type == 1) {
    //url should be "/hello/lookup/modelname.js?columns[column_id]=1"
  } else if (type == 2) {
    //url should be "/hello/lookup/modelname.js?columns[column_id]=2"
  } else {
    //url should be "/hello/lookup/modelname.js?columns[column_id]=6"
  }
};

change_lookup_number();

var sections = [
  {
    url: "",
  }
]

(function(){
  $.each(pcdfs, function(i, section) {
   console.log(section.url); // It's empty, I want to change this section.url
  }
})();

Any help or suggestions would be greatly appreciated!

Answer №1

Make sure to define the sections array before you try to access it in your function.

var sections = [
      {
        link: ""
      }
    ];

var updateLookupNumber = function() {
      var numType = document.getElementById('type_number').value;
      if (numType == 1) {
        sections[0].link = "/hello/lookup/modelname.js?columns[column_id]=1"
      } else {
        sections[0].link = "/hello/lookup/modelname.js?columns[column_id]=2"
      }
    };

updateLookupNumber();

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 showing information stored in an array on an Android app using C# and ListView

I am currently working on code in C# using Xamarin for Android. The code snippet is as follows: EditText nameText = FindViewById<EditText> (Resource.Id.editName); Button btnAddContact = FindViewById<Button> (Resource.Id.btnAddContact); ...

Retrieving Outdated Information through the Express Framework

Whenever I make a fetch request to a node express server, it sometimes returns old data that was previously fetched from the same endpoint. This issue occurs sporadically but seems to happen more often than not. Even after disabling Cache-Control in the f ...

The MEAN.JS platform encountered an issue with the Route.all() function, as it was expecting a callback function but instead

I am currently exploring the MEAN Stack and have been experimenting with meanjs.org yo generator in version 4.1. The CRUD example was included in the generated app, which I utilized as a reference for my own code. Upon execution, I encountered the followi ...

Search through a JSON object, identify all keys that start with the same letter, and transfer them to a new JSON data structure

I am looking to iterate through my JSON data and extract all keys along with their values that start with the letters "QM". Below is an example of my JSON content: [ { MHF46: 'Ledig', MHF181: '06', QM6_QMSI2: '1899-12-30 ...

Avoid Scroll Below Stuck Navigation

I've been searching for a solution to my issue for some time now, and while I've come across many articles discussing similar problems, none of them seem to address my specific problem. In my React app, I have a mobile version where users can ta ...

Issue with ng-click not functioning in Internet Explorer when selecting an option in AngularJS

I have the following HTML code snippet HTML: <select style="width: 100%" name="multipleSelect" id="multipleSelect" ng-model="data.multipleSelect" multiple> <option ng-click="BRPTab.AddFilesToOpenorDelete(fileName)" ng-repeat="fileName in BRP ...

Converting Revit coordinates to Viewer format

Is there a way to translate estimated coordinates from a Revit model into Viewer coordinates in order to accurately place a three.js object within the Viewer? ...

Updating Mysql through REST API/JWT using PUT method is not possible

I have been attempting to send an update request using Jwt (Tokens) and Node.Js with a backend in mysql. While Postman confirms that the record has been successfully updated, I am unable to locate where the actual update occurred. No changes seem to be ref ...

Getting the ajax response in PHP while editing a popup is a common requirement in web development

When the edit button is clicked, I want a popup to appear with all selected values displayed using ajax. My response is sent in this format: $data = array('cdid' => $model->cdid, 'cid' => $model->cid, 'icdcode' = ...

JSON variable unable to be stored in database

I have a Django application where I am attempting to save the configuration of a gridster widget as a JSON variable in the database. However, when I click the "Update" button on the webpage, no value is stored in the database. Below is my JavaScript code ...

Display a different image while another image is in the process of loading

I am currently facing an issue with a div that has a background image set up as follows: <div class="carousel-image" style="background: url(img/background.gif) no-repeat center center;"> </div> Upon loading my webpage, there is a brief ...

Navigation bar with a full-page slider

I am trying to incorporate my bootstrap nav bar into a full-width slider, similar to the layout in this example. https://i.sstatic.net/LE9wP.jpg This is the full width slider http://codepen.io/grovesdm/pen/MazqzQ Currently, I have positioned the nav ba ...

Steps to create a scrollable material-ui Modal

After setting up a Modal, I encountered an issue where the text describing my app inside the modal was overflowing, making it impossible to see the top and bottom parts. To solve this problem, I want to implement scroll functionality within the component s ...

Automated tool for generating random JSON objects

Looking for a tool that can generate random JSON objects? I'm in need of one to test my HTTP POST requests and incorporate the random JSON object into them. Any recommendations? ...

Displaying a PDF file in a browser using a byte array with JavaScript without the need for base64 encoding

I am currently dealing with an ASP .NET Web API where the POST function is sending back a byte array in C# code like this: return Ok(outputStream.ToArray()); outputStream is of type System.IO.MemoryStream and ToArray() returns a byte array. The purpose ...

"Although the stripe setup functions correctly in the dashboard, it is not working in the CLI or the

Experiencing an issue with the subscription process through Stripe, where subscriptions are successfully processed by Stripe but not reflected in Mongo for verification on the success page. Upon checking the Mongo collection, the isSubscribed status is no ...

The constant reloading of the page is hindering the crucial display of the data

After successfully getting something to work, I noticed that the data disappears when the page refreshes. How can I prevent this from happening? <html> <head> <meta charset="utf-8"> <title> IT Services </ti ...

"Engaging with the touchscreen inhibits the triggering of click

Within this div, I have implemented touch-action:pan-y;. Surrounding this div is an anchor tag. If you click on the div, the link will successfully redirect. However, if you swipe on the div and then click, the link won't work on the first attempt bu ...

transmitting information to Laravel via ajax

Is it possible to send a multi-step form data separately to the Laravel controller in order to store them into MySQL? An example would be sending the inputs data shown below: <ul> <li> <input type="radio" class="step1r1" value ...

The issue of a click event not triggering in JSDOM was giving me

I recently developed a small application using Vanilla JavaScript. In order to test the app, I have incorporated Jest and JSDOM. The app features a button that is supposed to delete a row from a table. The testing code is as follows: it('Table deletes ...