0

I need the output element of an html form to display content that is based off whatever item is checked from a radio button set. When I only need to display which value is checked it works fine, but when I try to use a function to switch the output, the if statement doesn't work - the function always returns 'Farenheit' as though option.value remains 'celcius' (regardless of how often the option buttons are swapped):

<script>
    function switchOpt(opt){
        if (opt='celcius'){
        return 'Farenheit';
        }else{
        return 'Celcius';
        }
    }
</script>
<form oninput="swap.value=switchOpt(option.value)">
    <input type="radio" name="option" value="celcius" checked /> Celcius <br />
    <input type="radio" name="option" value="farenheit" /> Farenheit <br />
    <output name="swap"></output>
</form>

The same setup for the if statement works in other functions, so I'm guessing the problem is that there's a datatype mismatch or something in how I'm trying to refer to the values here.

How do you refer to the checked value in a set of radio buttons within a function?

1
  • sidenote: Fahrenheit is the correct spelling. :) Commented Aug 10, 2018 at 10:49

2 Answers 2

2

function switchOpt(opt) {
  if (opt == 'farenheit') {
    return 'Farenheit';
  } else {
    return 'Celcius';
  }
}
<form oninput="swap.value=switchOpt(option.value)">
  <input type="radio" name="option" value="celcius" checked /> Celcius <br />
  <input type="radio" name="option" value="farenheit" /> Farenheit <br />
  <output name="swap"></output>
</form>

Note the change in the if condition...

=== operator checks both value and type whereas == only compares the value. For example, 3 == '3' will return true even though one is a string and the other is a number and to avoid this should use ===. Therefore, I’d say it’s safer to use the former although I did use == here :) Single = is for assigning. For example var x = document.getElementById(‘box’);

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

5 Comments

In light of the difference between these two answers, could you share why you’d use == instead of ===?
=== operator checks both value and type whereas == only concerns with the value. I’d say it’s safer to use the former although I did use == here :)
It’s for assigning. For example var x = document.getElementById(‘box’);
aaahhhhh! It all makes sense now! Now I want to give your answer the tick because of all the in-comments help you've added, even though the other one is a little more complete... Tell you what, Throw in this info into an update of your answer and you've got it.
Sorry, I should have been more elaborative - I've updated the answer now. Cheers
0

You should use === in your if clause instead of =.

In addition always add labels for your radio buttons and checkboxes.

<script>
    function switchOpt(opt){
        if (opt==='celcius'){
        return 'Farenheit';
        }else{
        return 'Celcius';
        }
    }
</script>
<form oninput="swap.value=switchOpt(option.value)">
    <input type="radio" name="option" id="celcius" value="celcius" checked /> <label for="celcius">Celcius</label> <br />
    <input type="radio" name="option" id="farenheit" value="farenheit" /><label for="farenheit"> Farenheit</label> <br />
    <output name="swap"></output>
</form>

2 Comments

In light of the difference between these two answers, could you share why you’d use === instead of ==?
true == 'true' will return true, true === 'true' will return false. === is strict comparison. Good practice is to use strict comparson, because == sometimes leads problems which are hard to analyze. Just enter === in google and you will read enough about this.

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.