So I'm allowing a user to input two locations:
HTML for input fields:
<input type="text" id="address1" placeholder="" />
<label>
<span>Pickup Address:</span>
</label>
<input type="text" id="address2" placeholder="" onfocusout="GetRoute()" />
<label>
<span>Dropoff Address:</span>
</label>
Which then shows them a div with the exact miles between the two distances and the overall minutes it should take once jQuery's .focusout() has been called with the exit of the second address field.
Corresponding HTML:
<div class="container-fluid" id="dv">
<div id="dvMap"></div>
<div class="row">
<div id="dvDistance">
<h3 id="miles">0</h3>
<h6>Over Miles</h6>
</div>
<div id="dvDistance">
<h3 id="time">0</h3>
<h6>Overall Time</h6>
</div>
</div>
</div>
The div and the values are initially hidden until focusout is called. Now everything works correctly until I want to update the initial value of an h2 tag. You see, the problem I'm running into is that I'm not able to concatenate the over value of the h2 tag with the update values after focusout has been called. It's giving me undefined when I call on the value with alert. This is what I'm trying to update:
HTML:
<div class="text-center">
<h2>Total amount $<span id="new_text">35</span></h2>
</div>
And lastly, here's my JS and jQuery:
$("#address2").focusout(function() {
$("#dv").slideDown("slow", function() {
document.getElementById('dv').style.display = "block";
});
doSomething();
});
function doSomething() {
var overallMiles = document.getElementById('miles').innerText;
var overallMins = document.getElementById('time');
alert(overallMiles);
// alert(overallMins);
document.getElementById("new_text").innerHTML = 35 + Number(overallMiles) + Number(overallMins);
}
A portion of GetRoute:
//*********DISTANCE AND DURATION**********************//
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix({
origins: [source],
destinations: [destination],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.IMPERIAL,
avoidHighways: false,
avoidTolls: false
}, function(response, status) {
if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
var distance = response.rows[0].elements[0].distance.text;
var duration = response.rows[0].elements[0].duration.text;
var dvMiles = document.getElementById("miles");
var dvTime = document.getElementById("time");
dvMiles.innerHTML = distance;
dvTime.innerHTML = duration;
} else {
alert("Unable to find the distance via road.");
}
});
}
I'm not wanting this value:
Any help is greatly appreciated!