0

I want to assign a drop down option to a unique ID so that when it is selected, the javascript will take that ID and turn it into a variable which I can use to display the cost.

This is what I have so far:

<form>
        <select>
        <option name ="Bristol "value="40.0" id="BN1">Bristol - Newcastle</option>
        </select>
        <button type="button" onclick="BookingFare(); return false;">Submit</button><br>
        </form>

function BookingFare() { 


            var price = 0;
            //var ofAdults = document.getElementById('adults').value;
            //var ofChildren = document.getElementById('children').value;
            var BN1 = document.getElementById('BN1').value;

            if (BN1) {
             var price = price + 40; 
            }

            document.getElementById('priceBox').innerHTML = price;

            }

I can't get it to display the price, even when I just try to get it to print out "hello" when BN1 is selected it doesn't work. Can anyone tell me what I'm missing?

2
  • Are you sure you have an element with the id of priceBox? Commented Mar 4, 2015 at 18:32
  • yes sorry just didnt show it in html code Commented Mar 4, 2015 at 18:49

1 Answer 1

1

function BookingFare() { 


            var price = 0;
            //var ofAdults = getElementById('adults').value;
            //var ofChildren = getElementById('children').value;
            var BN1 = document.getElementById('BN1').value;

            if (BN1) {
             var price = price + 40; 
            }

            document.getElementById('priceBox').innerHTML = price;

            }
<form>
        <select id="BN1">
        <option name ="Bristol "value="40.0" >Bristol - Newcastle</option>
        </select>
        <button type="button" onclick="BookingFare(); return false;">Submit</button><br>
        </form>

<label id='priceBox'></label>

Sign up to request clarification or add additional context in comments.

3 Comments

Assuming you have more than just one route in the select, you probably want price = BN1; in the if block, otherwise why bother specifying the price in the option elements.
Yeah just copied what the OP stated.. It's his discretion now
Okay yep i have noticed when i add more options in this way is no good i am sorry about that, i have done your way now price = BN1 in if statements but all it does is take the if statement at the bottom and use that price for whatever i use. Any suggestions?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.