Set the JavaScript array variable equal to the value of the input text field containing a JavaScript array

Currently, I am attempting to populate location data for Google Maps initialization. I have tried assigning an HTML input field with a JavaScript array format in order to use it for Google Maps, but unfortunately, my attempts have been unsuccessful. Here is the code snippet:

      <input id="a" name="a" value='["WAPDA Town, Lahore, Pakistan","31.4311985","74.26435820000006","1"],["Johar Town, Lahore, Pakistan","31.469693","74.27284610000004","2"],'>

Afterwards, I call it within my script to initialize it for locations.

 var location = '[' + document.getElementById('a').value + ']';

However, I am not getting the desired result and realize that I am doing something wrong. Can anyone suggest the proper way to initialize it?

Answer №1

Give it a shot (I took out the last comma from your input)

var location = JSON.parse(`[${a.value}]`);

console.log(location);
 <input id="a" name="a" value='["WAPDA Town, Lahore, Pakistan","31.4311985","74.26435820000006","1"],["Johar Town, Lahore, Pakistan","31.469693","74.27284610000004","2"]'>

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

Discovering the selected row with jqueryIs there a way to locate

There is a table with rows and a button that allows you to select a row: <table id="mytable" class="table-striped"> <tbody> <tr id="1"><td>Test1</td></tr> <tr id="2"><td>Test2</td>& ...

Maintain modifications in AngularJS modal even after closure

I have an HTML file with some AngularJS code for a modal window. <div ng-controller="ModalDemoCtrl"> <script type="text/ng-template" id="myModalContent.html"> <div class="modal-header"> <h3>I'm a modal!</h3> ...

Creating custom fields with user input in WordPress widgets

I'm currently working on a theme and I need to create dynamic input fields for a set of predefined labels in a custom widget. My goal is to achieve the following layout: CourseName FieldONE FieldTWO ----------------------------- ...

Select2 in Bootstrap does not trigger the select event when pasting until a word has been typed

One issue I encountered while using bootstrap select2 v4.* on my site (Yii 2.x) is that the event select2:select does not fire when text is pasted with ctrl+v until some text is manually typed. Once some text is entered, the event works as expected. What ...

Display or conceal elements using the unique identifier selected from a dropdown menu in JavaScript

I have been searching the internet for a solution to my issue but nothing seems to be working. Here is the problem: Unfortunately, I cannot modify the TR TD structure and am unable to use DIVs. I am trying to dynamically display certain TD elements based ...

Learning to read HTML tags using JavaScript is a valuable skill

I've been struggling with a problem for some time now and I really need help fixing it. Here's the issue: I have an asp:GridView where I store text with HTML tags in my database under an English column. During the DataBound event, I use Context. ...

Troubleshooting problems with dropdown binding in Knockout ObservableArray

I am encountering an issue with fetching data from a restful wcf service during page load and binding it to a dropdown, which is not functioning as expected. function CreateItem(name, value) { var self = this; self.itemNam ...

Render doesn't wait for the componentWillMount lifecycle method

I'm currently working on implementing a redirection to the home page for logged-in users. I'm using ping to fetch login information, setting a state based on the response, and then checking that state in the render method for redirection. However ...

What is the best way to pass a state within a route component in react-router?

... import { useNavigate, NavigateFunction } from "react-router"; ... function Form(): JSX.Element { const navigateToCountry = (country: string) => { // Code to navigate to country page with the given country } const [selectedCount ...

Problems arise when using AngularJS' .run function after navigating to a different page

I have encountered an issue with ngRoute while navigating between pages in my web application. The main login page is called index.html, and the routing is controlled by the main js file. However, I face a problem when trying to use a .run block on a speci ...

Delete dynamic elements from an array once they are no longer present in the DOM

In this code snippet, I have implemented a button that adds a new object to both the document and an array. When you click on each object, it gets removed from the document. However, the challenge here is how can we also remove it from the array? You can ...

The data retrieved from the API call is outdated

I am struggling with a weather web API that is only showing old data when called in the code. However, when I enter the API URL directly into the browser, it displays the most up-to-date information for the current city. Can anyone help me troubleshoot why ...

Can anyone tell me how to retrieve the value of {{org}} in this situation?

<head> <title>My Unique Page</title> </head> <body> <input type="text" ng-model="selectedOrg" ng-show="!isSuperAdmin" readonly /> <select id="nameOrg" ng-model="selectedOrg" ng-cha ...

Retrieve the child grid's data source in Kendo using Angular

I am currently working with a master detail grid in Kendo Grid and using the Angular version of the Kendo Grid. One interesting feature I have added is a custom button for adding a new record in each row of the grid. When this button is clicked, a new rec ...

Repetitive submissions of a form through Ajax post requests

Currently, I am utilizing this code to handle data sent via Ajax. Within the code, I am using the summernote editor. However, I have encountered an issue where if I submit the form while missing a required field, the form displays a 'required alert&ap ...

Leveraging the power of mapState and mapMutations within a namespaced module

I'm having trouble accessing mutations and states after dividing my Vuex store into three modules. I've attempted various syntaxes, but none seem to be working for me. MapStates: This is how I have set up the mapStates, with 'vendor' a ...

New update: Adjusting the chart by using the slider to modify the date values on the x-axis

Update: I have made changes to the question, the issue with values not appearing on the x-axis has been resolved. Now, as the slider is moved, the data on the graph remains constant and I can see changing x values. I am currently working on incorporating ...

Developing a TypeScript Library from Scratch

After following the instructions on this particular website, I have implemented the following JavaScript code: function A(id) { var about = { Version: "0.0.0.1", }; if (id) { if (window === this) { return new A(i ...

Creating a graph of the cosine function by tracking the movement of the mouse

Objective: Modify the length of the cosine wave based on the horizontal position of the mouse cursor. The key function to focus on is mouseMoveHandler. (function(window,document,undefined){ var canvas = document.getElementById('canvas'), rec ...

Exploring keys rather than values in Vue application

Currently, I am facing an issue where I am retrieving the values for three keys from my API using Axios. However, each time I make a request, the values are being displayed four times for each key. After removing one key from my data models and retesting, ...