Selecting ID

I am a relative beginner at JavaScript and I’m trying to work out how to update the price on my buy now form (you’ll need to click the link at the top of the page) from the country the user selects on the form.

At the moment I have:

<script>
    function getPrice() {
  var country = document.getElementById("country");
  if (country == "United Kingdom") {
    alert("£10 please");
else {
alert("£20 please");
  };
};
</script>

The theory being that if I select United Kingdom from , it would then alert “£10 please”. I don’t want an alert in the end - this was just my thinking of the easiest way to see if my code worked.

And it doesn’t.

Ideally I don’t want a full answer - I am kind of hoping someone might point me in the right direction - maybe I have done something simple that is wrong.

Thanks
James

You need to call your function getPrice() somewhere, but I can’t see that you are doing so.

1 Like

There’s also a bracket missing in your code.

‘country’ is the element and not the value of that element.

You probably want the value of country_name anyway as that will hold the chosen selection I believe.

1 Like

The check isn’t correct. You’d need to check the value property

grr…Ninja’d by @PaulOB again

2 Likes

Ok I’m getting somewhere. It now brings the alert box up when the home page is opened. Not quite what I wanted but the alert box works!

I have:

<script>
    function getPrice() {
  var country = document.getElementById("country_name");
  if (country == "United Kingdom") {
    alert("£10 please");
} else {
alert("£20 please");
  };
};
getPrice();
</script>

This line is still wrong. You want to check the value of the element (i.e. element.value), not the element itself

1 Like

I thought that by declaring it in a variable that would work?

var country = document.getElementById("country_name");

Therefore I am selecting the value of the country_name??

No you are selecting the element itself not its value.

The elements value will be element.val() ( jquery) (or element.value in js) as shown in the links we already gave :smile:

If you want the alert when the value is selected then look at calling the routine when a change happens.

1 Like

I don’t see where the country_name is being selected…

1 Like

Woooooooooohooooooooo I’ve done it! Thanks ever so much for your advice.

I ended up with:

<script>
    function getPrice() {
        var array = ["France", "Germany"];
        $("#country_name").change(function(){
  		if($(this).val() == "United Kingdom") {
  			alert("You have selected the United Kingdom");
                $('[name=os0]').val("UK");
  			} else if ($(this).val() == "France" || $(this).val() == "Germany" || $(this).val() == "Austria" || $(this).val() == "Belgium" || $(this).val() == "Bulgaria" || $(this).val() == "Croatia" || $(this).val() == "Cyprus" || $(this).val() == "Czech Republic" || $(this).val() == "Denmark" || $(this).val() == "Estonia" || $(this).val() == "Finland" || $(this).val() == "Greece" || $(this).val() == "Hungary" || $(this).val() == "Ireland" || $(this).val() == "Italy" || $(this).val() == "Latvia" || $(this).val() == "Lithuania" || $(this).val() == "Luxembourg" || $(this).val() == "Malta" || $(this).val() == "Netherlands" || $(this).val() == "Poland" || $(this).val() == "Portugal" ||$(this).val() ==  "Romania" || $(this).val() == "Slovakia" || $(this).val() == "Spain" || $(this).val() == "Sweden") {
                alert("You have selected Europe");
                $('[name=os0]').val("European Union");
            } else {
                alert("You have selected Elsewhere");
                $('[name=os0]').val("Rest Of The World");
            };
		});
};
getPrice();
</script>

I appreciate the European Union section isn’t the most elegant, I tried to use inarray() but couldn’t get it to work.

Thank you ever so much!

Now I just have to figure out how to get the damned button to work again!

1 Like

and get rid of the antiquated alert debugging calls - you don’t want someone using them to turn off JavaScript on your site.

Oh yeah good point - they were only there to test my JS was working.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.