I want to display my calculated value inside a span but it returns the value "Object HTMLSpanelement". I think it has to do something with my variable price being out of scope?
BTW: I use innerHTML because this is supported by older browsers, but what do you guys suggest? innerHTML or innerText?
Anyhoe, my code is:
function calcRoute() {
var start = document.getElementById("start").value;
var end = document.getElementById("end").value;
var distanceInput = document.getElementById("distance");
var hours = document.getElementById("timeH");
var minutes = document.getElementById("timeM");
var price = document.getElementById("priceCalculated");
document.getElementById("priceCalculated").innerHTML=price.toString();
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
distanceInput.value = parseFloat(response.routes[0].legs[0].distance.value / 1000).toPrecision(4);
<!-- Added this myself-->
hours.value = parseFloat(( response.routes[0].legs[0].duration.value / 3600) % 24).toPrecision(4);
minutes.value = parseFloat(( response.routes[0].legs[0].duration.value / 60) % 60).toPrecision(4);
<!-- END Adding-->
price.value = parseFloat( 2.97 + (2.18 * (response.routes[0].legs[0].distance.value / 1000)) + (0.36 * ((response.routes[0].legs[0].duration.value / 60) % 60))).toPrecision(4);
}
});
}
As you notice, I used the whole calculation of distanceInput and minutes instead of the variable names of these calculations to calculate my price. I did this because I got a NaN if I used the variable names instead of the whole calculation. But that is off topic.
My problem is at line 7 and 8.
added my HTML*
<div class="modal-content">
<div class="modal-header">
//bla
</div>
<div class="modal-body">
<p>
<div class="col-lg-4">
<input class="custom_label_left form-control" type="text" name="start" id="start" placeholder="Van..." />
</div>
<div class="col-lg-4">
<input class="custom_label_left form-control" type="text" name="end" id="end" placeholder="Naar..." />
</div>
<div class="col-lg-4">
<button type="button" class="custom_label_left form-control btn btn-info" type="submit" onclick="calcRoute()">Bereken prijs</button>
</div>
</p>
<p>
<div class="col-md-12">
<div class="col-sm-6">
<h3>Ritprijs elders: €<span class="label label-danger" id="priceCalculated" ></span></h3>
</div>
</div>
</p>
</div>
<div class="modal-body" id="map_canvas"></div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Afsluiten</button>
</div>
"[object HTMLSpanElement]", then your variable was pointing to the<span>and you tried to convert this to a String. You probably wanted the variable to hold the contents of the element instead, e.g. by accessing it's.textContentproperty