0

I can't figure out where I'm going wrong - when "Europe" is selected nothing happens.

When I take away the if statement it works, so the output code is definitely okay. The problem seems to be with the if condition.

 <script> 

 function shipping_calc() {  
    var val = document.getElementById.("country").value;    
    if(val === "Europe") {   
        document.getElementById("output").innerHTML = "£2.40";
    }   
 }

</script>

<select id="country" onchange="shipping_calc()">
  <option value="United Kingdom">United Kingdom</option>
  <option value="Europe">Europe</option>
  <option value="Worldwide">Worldwide</option>
</select>

<p id="output"></p>
1
  • 1
    You have document.getElementById.("country") (notice the period before ("country") Commented Apr 2, 2017 at 22:04

3 Answers 3

1

You had an extra . in your javascript. This works.

function shipping_calc() {

  var val = document.getElementById("country").value;

  if (val === "Europe") {
    document.getElementById("output").innerHTML = "£2.40";
  }
}
<select id="country" onchange="shipping_calc()">
  <option value="United Kingdom">United Kingdom</option>
  <option value="Europe">Europe</option>
  <option value="Worldwide">Worldwide</option>
</select>

<p id="output"></p>


You had document.getElementById.("country") and it should have been document.getElementById("country").

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

1 Comment

Of course it had to be something so simple. God dammit. Just takes a fresh set of eyes I guess. Thanks a bunch! :)
0

Try this...

var vall = document.getElementById("country");
var val = vall.options[vall.selectedIndex].value;

if (val === "Europe") {
  document.getElementById("output").innerHTML = "£2.40";
}

Comments

0

Here's a fiddle: https://jsfiddle.net/6dvgbjr0/

You had an error in your code, with an extra period:

var val = document.getElementById.("country").value;

Comments

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.