You are viewing limited content. For full access, please sign in.

Question

Question

Javascript to hide REJECT and show APPROVE or hide APPROVE and show REJECT

asked on May 20, 2016 Show version history

Hello everyone,

 

I am trying to use JS to hide buttons. Basically if a checkbox is selected I want to show one box, but if the checkbox isn't selected I want it to show a different one.

 

So by default I have one field hidden on document load: 

$('.Reject').hide();

 

Next is the code I am using:


    function changebuttons() {
        if ($("#q20 input:checked").val() == 'Yes') {
            $('.Reject ').show();
            $('.Approve').hide();
        } else $('.Reject').hide();
        $('.Approve').show();

Forgive me if there are syntax errors above, as I only copied a portion. The above code is showing the .Reject button, but is not hiding the .Approve button. Below is the entire JS I have written for this form.

//this code hides the next add another user field. It will show when one user is added.
$(document).ready(function() {
    $('.Reject').hide(); //hide the Comment + Add User(s) button by default  
    toggleFields(); //call this first so we start out with the correct visibility depending on the selected form values
    //this will call our toggleFields function every time the selection value changes
    $("#q21").change(function() {
        toggleFields();
    });
    $("#q20").change(function() {
        changebuttons();
    });
    //this toggles the visibility of our parent permission fields depending on the current selected value
    function toggleFields() {
        if ($("#q21 option:selected").text() == '') $("#q25, #q22")
            .hide();
        else $("#q25, #q22").show();
        //this code changes the button from Comment to Comment + Add User(s) if the add user to form is chosen
        changebuttons(); //call this first so we start out with the correct visibility depending on the selected form values
        //this will call our toggleFields function every time the selection value changes
    }

    function changebuttons() {
        if ($("#q20 input:checked").val() == 'Yes') {
            $('.Reject ').show();
            $('.Approve').hide();
        } else $('.Reject').hide();
        $('.Approve').show();
    }
});

Thanks in advance if we get a solution. Forgive me if it is a noob mistake :(

0 0

Replies

replied on May 21, 2016

Try surrounding the "else" clause in the changebuttons() function with braces. Without the surrounding braces, line 27 "$('.Approve').show();" will always run so the Approve button will always be displayed.

function changebuttons() {
    if ($("#q20 input:checked").val() == 'Yes') {
        $('.Reject ').show();
        $('.Approve').hide();
    } else {
        $('.Reject').hide();
        $('.Approve').show();
    }
}

 

2 0
You are not allowed to follow up in this post.

Sign in to reply to this post.