user9489641
user9489641

Reputation:

Javascript Unexpected Token { with conditionals

I am trying to put a JS conditional inside a listed order and have two radio buttons that I need to check and then proceed to the next list according to the selection.

<li data-input-trigger>
<label class="fs-field-label fs-anim-upper" for="q1">Welcome <br> <br> Is 
this application for yourself or someone else?</label>
<div class="fs-radio-group fs-radio-custom clearfix fs-anim-lower">
    <span><input id="radio_myself" name="radMyself" type="radio" 
    value="choice"/>Myself</span>
    <span><input id="radio_else" name="radElse" type="radio" 
    value="choice"/>Someone Else</span>
<script>                                 
    if(document.getElementById("radio_myself").checked == true){
    //do this;
    }                                    
    else(document.getElementById("radio_else").checked == true){
    //do that;
    }
</script>

So if radio_myself is selected I want it display the next list which is

<li>
    <label class="fs-field-label fs-anim-upper" for="q1">What's your name?</label>
</li>

or the other list if the other radio button is selected.

I am not sure if I am implementing it right as I just started with JS and HTML. I keep getting the unexpected token error { in the else line of code.

Upvotes: 0

Views: 115

Answers (2)

Alex Lakatos
Alex Lakatos

Reputation: 253

else does not accept a parameter. So you'll have to do another if inside the else if you want to check for that

if (document.getElementById("radio_myself").checked == true) {
  //do this;
} else {
  if (document.getElementById("radio_else").checked == true) {
    //do that;
  }
}

You can also check MDN, it's a really good resource if you want to look up how things work in JavaScript

Upvotes: 2

theblindprophet
theblindprophet

Reputation: 7937

if(document.getElementById("radio_myself").checked == true){
    //do this;
}
else(document.getElementById("radio_else").checked == true){
    //do that;
}

needs to be:

if(document.getElementById("radio_myself").checked == true){
    //do this;
}
else if(document.getElementById("radio_else").checked == true){
    //do that;
}

but you could rewrite as: (assuming at least one radio must be checked)

if(document.getElementById("radio_myself").checked){
    //do this;
}
else {
    //do that;
}

else cannot contain a conditional. It will get executed only if the if and else ifs above it are false.

Upvotes: 2

Related Questions