Hello I’ve been tearing my hair out over this all last night and this morning, I have a radio button set with 3 options and I have a variable called “Minimum_fare”. I’m trying to write javascript that will set the variable “minimum_fare” to a different number depending on which radio button is selected.
JavaScript
x
159
159
1
var countrycode="GB"
2
//Rate per km (number)
3
var rateperkm=1;
4
//Minimum fare (number)
5
var minimum_fare=110;
6
//Currrency Symbol
7
var currencysymbol="£";
8
//Avoid motorways / highways? true/false
9
var avoidHighways=false;
10
//Avoid toll roads? true/false
11
var avoidTolls=true;
12
//Show summary? true/false
13
var showsummary=false;
14
//Show Route Map
15
var showroutemap=true;
16
//rate per min
17
var rate_per_minute=0.916;
18
//API Key for map
19
var apikey="AIzaSyB_MrpX85obMpsk_eEdfE-iPIt06qbHyt0";
20
//----------End Settings--------------------------------
21
function initialize()
22
{
23
var options = {componentRestrictions: {country: countrycode}};
24
var input = /** @type {HTMLInputElement} */(document.getElementById('inputfrom'));
25
var searchBoxfrom = new google.maps.places.Autocomplete(input,options);
26
var input = /** @type {HTMLInputElement} */(document.getElementById('inputto'));
27
var searchBoxto = new google.maps.places.Autocomplete(input,options);
28
}
29
30
function ftn_estimate()
31
{
32
if (document.getElementById('inputfrom').value!="" && document.getElementById('inputto').value!="")
33
{
34
var origin = document.getElementById('inputfrom').value;
35
var destination = document.getElementById('inputto').value;
36
37
var service = new google.maps.DistanceMatrixService();
38
service.getDistanceMatrix(
39
{
40
origins: [origin],
41
destinations: [destination],
42
travelMode: google.maps.TravelMode.DRIVING,
43
unitSystem: google.maps.UnitSystem.METRIC,
44
avoidHighways: avoidHighways,
45
avoidTolls: avoidTolls,
46
}, callback);
47
}
48
}
49
50
function change_MinimumFare(sender){
51
minimum_fare==parseFloat(sender.value);
52
console.log('minimum_fare is : ',minimum_fare);
53
}
54
55
56
function callback(response, status) {
57
if (status != google.maps.DistanceMatrixStatus.OK) {
58
alert('Error was: ' + status);
59
} else {
60
var origins = response.originAddresses;
61
var destinations = response.destinationAddresses;
62
63
for (var i = 0; i < origins.length; i++) {
64
var results = response.rows[i].elements;
65
66
for (var j = 0; j < results.length; j++) {
67
68
69
70
if(showsummary)
71
{
72
document.getElementById('summary').innerHTML=origins[i] + ' to ' + destinations[j]
73
+ ': ' + results[j].distance.text + ' in '+ results[j].duration.text
74
}
75
document.getElementById('distance').value=(results[j].distance.value/1000).toFixed(1);
76
document.getElementById('time').value=(results[j].duration.value/60).toFixed(1);
77
78
79
80
81
var calc_fare=(results[j].distance.value/1000)*rateperkm;
82
83
84
85
if (calc_fare<16)
86
{
87
calc_fare=minimum_fare;
88
}
89
else calc_fare=calc_fare+minimum_fare;
90
91
92
93
94
document.getElementById('fare').value=currencysymbol+calc_fare.toFixed(2);
95
}
96
}
97
98
if (showroutemap)
99
{
100
var origin = document.getElementById('inputfrom').value;
101
var destination = document.getElementById('inputto').value;
102
getpath(origin,destination);
103
}
104
}
105
}
106
google.maps.event.addDomListener(window, 'load', initialize);
107
108
109
function getpath(a,b) {
110
var directionsService = new google.maps.DirectionsService();
111
var directionsDisplay = new google.maps.DirectionsRenderer({
112
preserveViewport: true
113
});
114
115
directionsService.route({
116
origin: a,
117
destination:b,
118
travelMode: google.maps.TravelMode.DRIVING
119
}, function(response, status) {
120
if (status === google.maps.DirectionsStatus.OK) {
121
// directionsDisplay.setDirections(response);
122
var polyline = new google.maps.Polyline({
123
path: [],
124
strokeColor: '#0000FF',
125
strokeWeight: 3
126
});
127
128
var legs = response.routes[0].legs;
129
130
for (i = 0; i < legs.length; i++) {
131
var steps = legs[i].steps;
132
for (j = 0; j < steps.length; j++) {
133
var nextSegment = steps[j].path;
134
for (k = 0; k < nextSegment.length; k++) {
135
polyline.getPath().push(nextSegment[k]);
136
}
137
}
138
}
139
140
do{
141
var newpath = [];
142
for (k = 0; k < polyline.getPath().length; k += 2) {
143
newpath.push(polyline.getPath().getAt(k));
144
}
145
polyline.setPath(newpath);
146
}
147
while (polyline.getPath().length>1000)
148
149
var path = polyline.getPath();
150
var encodeString = google.maps.geometry.encoding.encodePath(path);
151
152
153
document.getElementById('mapspan').innerHTML='<center><img src="https://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:3%7Ccolor:red%7Cenc:'+encodeString+'&key='+apikey+'"/></center>';
154
155
} else {
156
window.alert('Directions request failed due to ' + status);
157
}
158
});
159
}
JavaScript
1
72
72
1
<div id="mileage2" class="mileage2">
2
3
<form id="myform">
4
<div class="row content">
5
<div class="col-lg-5">
6
<label for="inputfrom" class="visuallyhidden">Input start Postcode</label>
7
<input id="inputfrom" type="text" placeholder="From Postcode">
8
9
<br />to<br />
10
11
<label for="inputto" class="visuallyhidden">Input destination postcode</label>
12
<input id="inputto" type="text" placeholder="To Postcode">
13
<br />
14
15
<input type="button" onclick="ftn_estimate();" value="Get route" />
16
17
<br /><br />
18
<table>
19
<tr>
20
<td>Time (mins)</td>
21
<td>
22
<input id="time" readonly type="text" placeholder="--" style="width:100px"></td>
23
</tr>
24
<tr>
25
<td>Distance (km)</td>
26
<td>
27
<input id="distance" readonly type="text" placeholder="--" style="width:100px"></td>
28
</tr>
29
<tr>
30
<td>Your Quote: </td>
31
<td>
32
<input id="fare" readonly type="text" placeholder="--" style="width:100px"></td>
33
</tr>
34
</table>
35
</div>
36
<div class="col-lg-3">
37
<fieldset id="menfield">
38
<div>
39
<input type="radio" id="1man" name="men" value="110"
40
checked onclick="change_MinimumFare(this)">
41
<label for="1man">1 Man</label>
42
</div>
43
44
<div>
45
<input type="radio" id="2man" name="men" value="160" onclick="change_MinimumFare(this)">
46
<label for="2man">2 Man</label>
47
</div>
48
49
<div>
50
<input type="radio" id="2man" name="men" value="200" onclick="change_MinimumFare(this)">
51
<label for="3man">3 Man</label>
52
</div>
53
</fieldset>
54
<script type="text/javascript">
55
function change_MinimumFare(sender){
56
minimum_fare=parseFloat(sender.value);
57
console.log('minimum_fare is : ',minimum_fare);
58
}
59
</script>
60
61
</div>
62
</div>
63
64
</form>
65
<span id="summary"></span>
66
<span id="mapspan"></span>
67
</div>
68
69
<script
70
src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry&key=AIzaSyB_MrpX85obMpsk_eEdfE-iPIt06qbHyt0"
71
>
72
</script>
I’ve looked at dozens of answers on stackexchange and tried implementing everything I can think of but I’m still stuck. How can I get the radio button to set the minimum fare variable?
Snippet updated
Advertisement
Answer
Does this answer your question? (reference)
JavaScript
1
160
160
1
var countrycode="GB"
2
//Rate per km (number)
3
var rateperkm=1;
4
//Minimum fare (number)
5
var minimum_fare=110;
6
//Currrency Symbol
7
var currencysymbol="£";
8
//Avoid motorways / highways? true/false
9
var avoidHighways=false;
10
//Avoid toll roads? true/false
11
var avoidTolls=true;
12
//Show summary? true/false
13
var showsummary=false;
14
//Show Route Map
15
var showroutemap=true;
16
//rate per min
17
var rate_per_minute=0.916;
18
//API Key for map
19
var apikey="AIzaSyB_MrpX85obMpsk_eEdfE-iPIt06qbHyt0";
20
//----------End Settings--------------------------------
21
function initialize()
22
{
23
var options = {componentRestrictions: {country: countrycode}};
24
var input = /** @type {HTMLInputElement} */(document.getElementById('inputfrom'));
25
var searchBoxfrom = new google.maps.places.Autocomplete(input,options);
26
var input = /** @type {HTMLInputElement} */(document.getElementById('inputto'));
27
var searchBoxto = new google.maps.places.Autocomplete(input,options);
28
}
29
30
function ftn_estimate()
31
{
32
if (document.getElementById('inputfrom').value!="" && document.getElementById('inputto').value!="")
33
{
34
var origin = document.getElementById('inputfrom').value;
35
var destination = document.getElementById('inputto').value;
36
37
var service = new google.maps.DistanceMatrixService();
38
service.getDistanceMatrix(
39
{
40
origins: [origin],
41
destinations: [destination],
42
travelMode: google.maps.TravelMode.DRIVING,
43
unitSystem: google.maps.UnitSystem.METRIC,
44
avoidHighways: avoidHighways,
45
avoidTolls: avoidTolls,
46
}, callback);
47
}
48
}
49
50
51
52
53
function callback(response, status) {
54
if (status != google.maps.DistanceMatrixStatus.OK) {
55
alert('Error was: ' + status);
56
} else {
57
var origins = response.originAddresses;
58
var destinations = response.destinationAddresses;
59
60
for (var i = 0; i < origins.length; i++) {
61
var results = response.rows[i].elements;
62
63
for (var j = 0; j < results.length; j++) {
64
65
66
67
if(showsummary)
68
{
69
document.getElementById('summary').innerHTML=origins[i] + ' to ' + destinations[j]
70
+ ': ' + results[j].distance.text + ' in '+ results[j].duration.text
71
}
72
document.getElementById('distance').value=(results[j].distance.value/1000).toFixed(1);
73
document.getElementById('time').value=(results[j].duration.value/60).toFixed(1);
74
75
76
77
78
var calc_fare=(results[j].distance.value/1000)*rateperkm;
79
80
81
82
if (calc_fare<16)
83
{
84
calc_fare=minimum_fare;
85
}
86
else calc_fare=calc_fare+minimum_fare;
87
88
89
90
91
document.getElementById('fare').value=currencysymbol+calc_fare.toFixed(2);
92
}
93
}
94
95
if (showroutemap)
96
{
97
var origin = document.getElementById('inputfrom').value;
98
var destination = document.getElementById('inputto').value;
99
getpath(origin,destination);
100
}
101
}
102
}
103
google.maps.event.addDomListener(window, 'load', initialize);
104
105
106
function getpath(a,b) {
107
var directionsService = new google.maps.DirectionsService();
108
var directionsDisplay = new google.maps.DirectionsRenderer({
109
preserveViewport: true
110
});
111
112
directionsService.route({
113
origin: a,
114
destination:b,
115
travelMode: google.maps.TravelMode.DRIVING
116
}, function(response, status) {
117
if (status === google.maps.DirectionsStatus.OK) {
118
// directionsDisplay.setDirections(response);
119
var polyline = new google.maps.Polyline({
120
path: [],
121
strokeColor: '#0000FF',
122
strokeWeight: 3
123
});
124
125
var legs = response.routes[0].legs;
126
127
for (i = 0; i < legs.length; i++) {
128
var steps = legs[i].steps;
129
for (j = 0; j < steps.length; j++) {
130
var nextSegment = steps[j].path;
131
for (k = 0; k < nextSegment.length; k++) {
132
polyline.getPath().push(nextSegment[k]);
133
}
134
}
135
}
136
137
do{
138
var newpath = [];
139
for (k = 0; k < polyline.getPath().length; k += 2) {
140
newpath.push(polyline.getPath().getAt(k));
141
}
142
polyline.setPath(newpath);
143
}
144
while (polyline.getPath().length>1000)
145
146
var path = polyline.getPath();
147
var encodeString = google.maps.geometry.encoding.encodePath(path);
148
149
150
document.getElementById('mapspan').innerHTML='<center><img src="https://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:3%7Ccolor:red%7Cenc:'+encodeString+'&key='+apikey+'"/></center>';
151
152
} else {
153
window.alert('Directions request failed due to ' + status);
154
}
155
});
156
}
157
function change_MinimumFare(sender){
158
minimum_fare=parseFloat(sender.value);
159
console.log('minimum_fare is : ',minimum_fare);
160
}
JavaScript
1
27
27
1
<fieldset id="menfield">
2
<div>
3
<input type="radio" id="1man" name="men" value="110"
4
checked onclick="change_MinimumFare(this)">
5
<label for="1man">1 Man</label>
6
</div>
7
8
<div>
9
<input type="radio" id="2man" name="men" value="160" onclick="change_MinimumFare(this)">
10
<label for="2man">2 Man</label>
11
</div>
12
13
<div>
14
<input type="radio" id="2man" name="men" value="200" onclick="change_MinimumFare(this)">
15
<label for="3man">3 Man</label>
16
</div>
17
</fieldset>
18
19
<script type="text/javascript">
20
function change_MinimumFare(sender){
21
minimum_fare=parseFloat(sender.value);
22
console.log('minimum_fare is : ',minimum_fare);
23
}
24
</script>
25
<script
26
src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry&key=AIzaSyB_MrpX85obMpsk_eEdfE-iPIt06qbHyt0"
27
>
I have added function change_MinimumFare
to both snippets since it requires full code to run
Edited to Add:
added parseFloat
function since calc_fare
is a float
variable