Chris
Chris

Reputation: 3

JavaScript if statement from select box

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>

Upvotes: 0

Views: 4479

Answers (3)

blackandorangecat
blackandorangecat

Reputation: 1344

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").

Upvotes: 1

jas7457
jas7457

Reputation: 1782

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;

Upvotes: 0

Konstantinos
Konstantinos

Reputation: 973

Try this...

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

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

Upvotes: 0

Related Questions