Angel
Angel

Reputation: 357

Condition with radiobutton checked don't work

I have 2 radio buttons in fieldset. I'm try to apply condition if the second radio is checked, but it doesn't work. If I get the two elements by ID using firebug console, it says that the 2 elements are checked.

My code:

<fieldset data-role="controlgroup" data-type="horizontal" data-role="fieldcontain">
    <input type="radio" name="soci1" id="sisoci1" value="soci1"/>
    <label for="sisoci1">Soci/Abonat</label>

    <input type="radio" name="soci1" id="nosoci1" value="nosoci1"/>
    <label for="nosoci1">Convidat</label>
</fieldset>

My function:

  if(document.getElementById("nosoci1").checked=true){
        check_soci1="no_soci";
    }else{
        check_soci1="soci";
    }

What the firebug says:

  >>> document.getElementById("sisoci1")
    <input id="sisoci1" type="radio" value="soci1" name="soci1" checked="checked">
    >>> document.getElementById("nosoci1")
    <input id="nosoci1" type="radio" value="nosoci1" name="soci1" checked="checked">

Can anyone help me? Thanks.

Upvotes: 0

Views: 2122

Answers (1)

Aleks G
Aleks G

Reputation: 57316

In order to use comparison, you need double == sign:

if(document.getElementById("nosoci1").checked == true) { ... }

Or just skip the comparison altogether:

if(document.getElementById("nosoci1").checked) { ... }

The way your code is written, with single = sign, 'checked' property is assigned the value of true and then if comparison is executed on that value - effectively always evaluating to true.

Upvotes: 2

Related Questions