0

I am having 3 radio buttons approved and rejected and 4 text fields and text boxes..

If i am clicking r1, all the text field and text boxes should be hided. but if i am clicking r2 and r3 the text field and text box should be displayed..

This is the code which i have used

if(approved.checked == true) {

  reason.style.visibility="hidden";

  Reason.style.visibility="hidden";
  sanction.style.visibility="display";
  sanctioned.style.visibility="display";
}

if(rejected.checked == true) {
    reason.disabled =false;
  reason.style.display="none";
  Reason.style.display="none";
  sanction.style.visibility="hidden";
  sanctioned.style.visibility="hidden";
}

This code is working for the first time when i click those radio buttons. But if am clicking rejected and then clicking approved it is not working.

4
  • 1
    Please try to use the formatting tool when posting code samples. (fixed it for you this time) :] Commented Nov 23, 2011 at 5:07
  • Hmm. display:none and visibility:hidden are not completely analogous. I'd use one or the other, but not both. Commented Nov 23, 2011 at 5:10
  • @TiesonT. I tried without using display:none also. Even then it was not working Commented Nov 23, 2011 at 5:12
  • The title is no way related to the question you have. Commented Nov 23, 2011 at 5:12

3 Answers 3

2

Don't mix the display and visibility properties, use one or the other. Valid values for the display property are: inline | block | list-item | inline-block | table | inline-table | table-row-group | table-header-group | table-footer-group | table-row | table-column-group | table-column | table-cell | table-caption | none | inherit

Valid values for visibility are: visible | hidden | collapse | inherit

The short answer: use visible instead of display.

So your code can be (assuming the variables are references to suitable DOM elements):

if(approved.checked) {
  reason.disabled = true;   // keep symetry with next loop
  reason.style.visibility = "hidden";
  Reason.style.visibility = "hidden";
  sanction.style.visibility = "visible";
  sanctioned.style.visibility = "visible";
}

if(rejected.checked) {
  reason.disabled = false;
  reason.style.visibility = "hidden";
  Reason.style.visibility  = "hidden";
  sanction.style.visibility = "hidden";
  sanctioned.style.visibility = "hidden";
}

Note that it is simpler to hide the form controls by putting them inside another element (say a div) and hiding that. But why not just disable them? Hiding and showing them might annoy, distract or just plain confuse users.

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

1 Comment

This is working fine. Thanks for your valuable quick suggestion.
0

in the first instead of reason.style.visibility="hidden you can use a jQuery function .hide() and .show(),

<script type="text/javascript">
    $(function () {

        $('input[type="radio"]').change(function () {
            if (this.id == r1) { 
                $('#textes').hide();
            }
            else
            {
                $('#textes').shwo();
            }
        })
        if (approved.checked == true)

    })
</script>

<div id="textes">
/*your chack  text fields and text boxes..*/
</div>

http://jsfiddle.net/rEK87/1/

2 Comments

in my answer I use jQuery, do you know jQuery?
I have not yet used jQuery. So if you could give up with a solution in JavaScript that would be greatfull..
0

Per your examples and our comments above, is this what you are after?

Markup:

<form>
    <input type="radio" name="rdo" id="r1" />
    <label for="r1">Approved</label>
    <input type="radio" name="rdo" id="r2" />
    <label for="r2">Rejected</label>
</form>
<div id="accepted">
    <p>This is the accepted block.</p>
</div>
<div id="rejected">
    <p>This is the rejected block.</p>
</div>

CSS:

#accepted, #rejected
{
    display:none;
}

jQuery:

$(function(){
    $('#r1').click(function(e){
        $('#accepted').show();
        $('#rejected').hide();
    }); 

        $('#r2').click(function(e){
        $('#accepted').hide();
        $('#rejected').show();
    }); 
});

http://jsfiddle.net/rEK87/

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.