Dynamic value in API url call

I am trying to pull a dynamic value that is gathered in a form when the user drags the marker in Google Maps. I have broken down my API URL but have not been successful in getting the dynamic value to the URL. Inspection with Dev Tools shows the form value updating when the marker is moved. Also, no errors are occurring when I put a static value in for the var apilat and var apilng

I sure could use some help in properly getting the form values to the URL.

Script Snippet

function latfunction() {
var getlat = document.getElementById("lat").method;
var getlng = document.getElementById("lng").method;

var api = 'https://api.worldweatheronline.com/premium/v1/marine.ashx?&q=';
var apilat = 'getlat';
var apilng = 'getlng';
var apikey = '&key=c45390b6209242f4941174356200201';
var format = '&format=json';
var url = api + apilat + apilng + apikey + format;

$.getJSON(url, function(data){

Here is a screenshot of the location of the form values needed, inside the black box.


So, first thing to look at: the latFunction() isn’t going to do what you think. Also, it’s not going to be available inside your snippet unless the function actually gets called. Then, there are string building errors in here:

Presumably, getlat and getlng are what you’re using for your latitude and longitude, though what the method property on a text input should be, I don’t know. Inputs have a value attribute, and that is more likely what you’re looking for.

Now, the way that string should look when done is not what you’re getting in the console. In particular there’s an error in the part that builds the lat/long portion. By the API documentation, that part should look like &q=<lat>,<long> (substituting your values for lat and long). Let’s say that we were using lat=39.531 and lng=-73.108 - what we should see is &q=39.531,-73.108, while what you’re actually seeing is &q=getlatgetlng - you are setting apilat to the string “getlat”, not the variable getlat.

I would suggest you look at Template Literals, a way to write the string you want, with markers in place for the variables you want to insert. In this case, it would be a very clean, easy way to write that url string.