Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
23
rated 0 times [  30] [ 7]  / answers: 1 / hits: 15441  / 10 Years ago, mon, october 6, 2014, 12:00:00

I am calling a function called getPrice() in javascript file by using onChange() command in tag in HTML file, but no matter what I do or fix, it still doesnt show the price. I have done a lot of research but cant seem to find any correct solutions. Helps will be greatly appreciated.



Here is my HTML code:



    <body>
<form action=# name=ITSbookform id=ITSform>
<fieldset>
<legend>Into The Storm Booking</legend>
<label>Choose your cinema</label><br>
<select id=selectedCinema>
<option>--Select--</option>
<option value=maximaCinema>Maxima Cinema</option>
<option value=rivolaCinema>Rivola Cinema</option>
</select><br>
<label>Select day and time</label>

<select id=dayAndTime>

</select>
<br>
<label>Select Seat</label>
<select id=selectedSeat onchange=getPrice()>

</select>
<p id=total>Total Price: </p>


</form>
</fieldset>

</body>




and the Javascipt part:



    function getPrice(){
var totalPrice = 0;
if(document.getElementById(selectedCinema).value == maximaCinema){
if(document.getElementById(dayAndTime).value == 9pmMonday || document.getElementById(dayAndTime).value == 9pmTuesday){
seatPrice[full] = 12;
seatPrice[conc] = 10;
seatPrice[child] = 8;
seatPrice[FirstClass-Adult] = 25;
seatPrice[FirstClass-Child] = 20;
seatPrice[beanbag] = 20;
}
else{
seatPrice[full] = 18;
seatPrice[conc] = 15;
seatPrice[child] = 12;
seatPrice[FirstClass-Adult] = 30;
seatPrice[FirstClass-Child] = 25;
seatPrice[beanbag] = 30;
}
}
else{
seatPrice[adult] = 18;
seatPrice[conc] = 15;
seatPrice[child] = 12;
}

var seat = theForm.elements[selectedSeat];
totalPrice = seatPrice[seat.value];
document.getElementById(total).innerHTML = $ + totalPrice;
}


and here is the full version of the code in JSFiddle: http://jsfiddle.net/stb0v5Ln/



Thank you.


More From » jquery

 Answers
196

  1. use jQuery consistently, your error is using theForm instead of accessing the form again or accessing the form field by ID

  2. change the fiddle to head instead of onload



FIDDLE



var seat = $(#selectedSeat);
totalPrice = seatPrice[seat.val()];
$(#total).html($ + totalPrice);


Also remove the onchange from the tag and add



$(#selectedSeat).change(getPrice);


to the ready



ps: remove all head and body tags when creating a fiddle


[#69233] Thursday, October 2, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arthur

Total Points: 729
Total Questions: 107
Total Answers: 109

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;