I have a question regarding a problem I've having with my Javascript coding.
The code is as follows:
function PizzaSize(form) {
if (form.size.value = "medium") {
alert("A medium size pizza has been selected.")
}
if (form.size.value = "large") {
alert("A large size pizza has been selected.")
}
}
<form name="pizzaorder">
<b><u>Size Selection</b>
</u>
<br>
<input type="radio" name="size" value="medium" onclick="PizzaSize(this.form)">Medium
<br>
<input type="radio" name="size" value="large" onclick="PizzaSize(this.form)">Large
<br>
</form>
So, when one of these radio buttons is clicked I direct it to run the following function:
However, when I test the code in a browser, it doesn't work right. When I click the "Medium" radio button, I get a message stating "A medium size pizza has been selected." When I click OK on the message, the radio button value then changes so it selects a large size pizza instead, and then I get a message saying "A large size pizza has been selected."
Does anyone know how I can fix this problem? I'd like to keep the function, but hopefully modify it so that it works correctly instead of how it's working as of now. Any help would be appreciated.
you are using assignment operator =
to check equality which is wrong.
use equality operator ==
to compare e.g.
if (form.size.value = "large")
{
alert ("A large size pizza has been selected.")
}
should be
if (form.size.value == "large")
{
alert ("A large size pizza has been selected.")
}