0

I am creating some radio buttons and when I click on one of them some fields appear. My problem is that when I click to the next button the fields from the previous one stay on screen while I need them to disappear. This is the code I have written, I know that the reason of my failure is that it never enters the else statement in my javascript function. Can someone help me, I am a rookie in these two languages, is there a way to keep the previous value sent to the function so that I can compare with it for the else statement? Any other solution would also be well received!

<HTML>
<HEAD> <title> Ticket Choice </title></HEAD>
<BODY>
<FORM>
<SCRIPT type="text/javascript">
    function IsCheck(but_Check, if_Check){
        if (document.getElementById(but_Check).checked) {
            document.getElementById(if_Check).style.display = 'block';
        }
        else document.getElementById(if_Check).style.display = 'none';
    }
</SCRIPT>

<input type = "radio" onclick = "javascript:IsCheck(&quot;buttonCheck1&quot;,&quot;ifChecked1&quot;);" name = "plane" id = "buttonCheck1"> PLANE <br>
    <div id = "ifChecked1" style="display:none">
        <label for = "depart">Departure:</label> 
        <input type = "text" id = "depart" name = "depart_plane" maxlength = "200"><br>
        <label for = "dest">Destination:</label> 
        <input type = "text" id = "dest" name = "destination_plane" maxlength = "200"><br>
    </div>

<input type = "radio" onclick = "javascript:IsCheck(&quot;buttonCheck2&quot;,&quot;ifChecked2&quot;);" name = "plane" id = "buttonCheck2"> SHIP <br>
    <div id = "ifChecked2" style="display:none">
        <label for = "depart">Departure:</label> 
        <input type = "text" id = "depart" name = "depart_ship" maxlength = "200"><br>
        <label for = "dest">Destination:</label> 
        <input type = "text" id = "dest" name = "destination_ship" maxlength = "200"><br>
    </div>

<input type = "radio" onclick = "javascript:IsCheck(&quot;buttonCheck3&quot;,&quot;ifChecked3&quot;);" name = "plane" id = "buttonCheck3"> O.S.E <br>
    <div id = "ifChecked3" style="display:none">
        <label for = "depart">Departure:</label> 
        <input type = "text" id = "depart" name = "depart_ose" maxlength = "200"><br>
        <label for = "dest">Destination:</label> 
        <input type = "text" id = "dest" name = "destination_ose" maxlength = "200"><br>
    </div>
</FORM>
</BODY>
</HTML>

1 Answer 1

1

The condition will never enter on else statement, because always you click on a radio, it will be checked.

Instead, try this:

<input type = "radio" onclick = "IsCheck(1);" name = "plane" id = "buttonCheck1"> PLANE <br>
<div id = "ifChecked1" style="display:none">
    <label for = "depart">Departure:</label>
    <input type = "text" id = "depart" name = "depart_plane" maxlength = "200"><br>
    <label for = "dest">Destination:</label>
    <input type = "text" id = "dest" name = "destination_plane" maxlength = "200"><br>
</div>
<input type = "radio" onclick = "IsCheck(2);" name = "plane" id = "buttonCheck2"> SHIP <br>
<div id = "ifChecked2" style="display:none">
    <label for = "depart">Departure:</label>
    <input type = "text" id = "depart" name = "depart_ship" maxlength = "200"><br>
    <label for = "dest">Destination:</label>
    <input type = "text" id = "dest" name = "destination_ship" maxlength = "200"><br>
</div>
<input type = "radio" onclick = "IsCheck(3);" name = "plane" id = "buttonCheck3"> O.S.E <br>
<div id = "ifChecked3" style="display:none">
    <label for = "depart">Departure:</label>
    <input type = "text" id = "depart" name = "depart_ose" maxlength = "200"><br>
    <label for = "dest">Destination:</label>
    <input type = "text" id = "dest" name = "destination_ose" maxlength = "200"><br>
</div>

JavaScript:

function IsCheck(number){
//here we iterate the three divs
for(i = 1; i <= 3; i++){
    //if the current number (i) is different to the checked input (number)
    if (i != number) {
        document.getElementById('ifChecked'+i).style.display = 'none';
    }
    else {
        document.getElementById('ifChecked'+i).style.display = 'block';
    }
}}
Sign up to request clarification or add additional context in comments.

Comments

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.