1

I have the following code:

function showAccessRequests_click() {
    var buttonValue = $("#showAccessRequests").val();
    if (buttonValue == "Show") {
        $(".hideAccessRequest").removeClass("hideAccessRequest");
        $("#showAccessRequests").val("Hide");
    }
    else {
        $(".hideAccessRequest").addClass("hideAccessRequest");
        $("#showAccessRequests").val("Show");
    }
}

This script removes a class fine but it does not want to add the class. Can you see any issues with this code?

3 Answers 3

6

When you add hideAccessRequest class to the element, you search for it by the existence of that class.. if you are adding it, that class won't already be applied and thus you won't match any elements.

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

1 Comment

Oops my bad...should've seen that :P
1

$(".hideAccessRequest") doesn't exist. you need to use id, I guess. And you might want to look at toggleClass.

Comments

0

you'd need an identifier for the classes you want to toggle ex:"accessRequest"... try this.

function showAccessRequests_click() {
    var buttonValue = $("#showAccessRequests").val();
    if (buttonValue == "Show") {
        $(".accessRequest").removeClass("hideAccessRequest");
        $("#showAccessRequests").val("Hide");
    }
    else {
        $(".accessRequest").addClass("hideAccessRequest");
        $("#showAccessRequests").val("Show");
    }
}

classes are space-delimited, so if you want them hidden by default...

<div class="accessRequest hideAccessRequest">...</div>

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.