1

I am trying to build a simple toggle button to change the classes on an element and its text of the containing 'p' element.

On the first click it works just fine, but then it doesn't toggle back.

What do I miss here?

$('.disclaimer').click(function() {
    if ($('#disclaimerText').text = "Disclaimer Declined") {
        $('.disclaimer').addClass("disclaimerAccepted");
        $('#disclaimerText').text("Disclaimer Accepted")
    } else {
        $('.disclaimer').removeClass("disclaimerAccepted");
        $('#disclaimerText').text("Disclaimer Declined")
    }
});
.disclaimer {
    font-family: 'Varela Round', sans-serif;
    font-weight: bold;
    font-size: 0.7vw;
    width: 15%;
    height: 50px;
    border-radius: 20px;
    background-color: #f96c8a;
    color: #545d7b;
}

.disclaimerAccepted {
    font-family: 'Varela Round', sans-serif;
    font-weight: bold;
    font-size: 0.7vw;
    width: 15%;
    height: 50px;
    border-radius: 20px;
    background-color: #30e83a;
    color: #545d7b;
}

#disclaimerText {
    display: block;
    margin: auto;
    font-size: 2vw;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="disclaimer">
    <p id="disclaimerText">Disclaimer Declined</p>
</div>

2 Answers 2

2

You can use jQuery's .toggleClass():

Add or remove one or more classes from each element in the set of matched elements, depending on either the class's presence or the value of the state argument.

Please Note: text is a jQuery method, to get the text you have to use .text() and also instead of assignment operator (=), you have to use comparison operator (== or ===) to compare the text:

$('.disclaimer').click(function() {
  $('.disclaimer').toggleClass("disclaimerAccepted");
  if ($('#disclaimerText').text() == "Disclaimer Declined") {        
    $('#disclaimerText').text("Disclaimer Accepted")
  } else {
    $('#disclaimerText').text("Disclaimer Declined")
  }
});
.disclaimer {
    font-family: 'Varela Round', sans-serif;
    font-weight: bold;
    font-size: 0.7vw;
    width: 15%;
    height: 50px;
    border-radius: 20px;
    background-color: #f96c8a;
    color: #545d7b;
}

.disclaimerAccepted {
    font-family: 'Varela Round', sans-serif;
    font-weight: bold;
    font-size: 0.7vw;
    width: 15%;
    height: 50px;
    border-radius: 20px;
    background-color: #30e83a;
    color: #545d7b;
}

#disclaimerText {
    display: block;
    margin: auto;
    font-size: 2vw;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="disclaimer">
  <p id="disclaimerText">Disclaimer Declined</p>
</div>

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

2 Comments

And how to additionally change the text on toggle/click? Thanks
Sorry, I mean I want the text to change to "Disclaimer Accepted" or "Disclaimer Declined" depending on the toggle status (so changing text back and forth on each click of the div)
2

You wrong at text() instead of text, and missing == instead of = is assign operator

Change from

if ($('#disclaimerText').text = "Disclaimer Declined") {

to

 if ($('#disclaimerText').text() == "Disclaimer Declined") {

$('.disclaimer').click(function() {
    if ($('#disclaimerText').text() == "Disclaimer Declined") {
    
        $('.disclaimer').addClass("disclaimerAccepted");
        $('#disclaimerText').text("Disclaimer Accepted")
    } 
    else {
       
        $('.disclaimer').removeClass("disclaimerAccepted");
        $('#disclaimerText').text("Disclaimer Declined")
    }
});
.disclaimer {
    font-family: 'Varela Round', sans-serif;
    font-weight: bold;
    font-size: 0.7vw;
    width: 15%;
    height: 50px;
    border-radius: 20px;
    background-color: #f96c8a;
    color: #545d7b;
}

.disclaimerAccepted {
    font-family: 'Varela Round', sans-serif;
    font-weight: bold;
    font-size: 0.7vw;
    width: 15%;
    height: 50px;
    border-radius: 20px;
    background-color: #30e83a;
    color: #545d7b;
}

#disclaimerText {
    display: block;
    margin: auto;
    font-size: 2vw;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="disclaimer">
    <p id="disclaimerText">Disclaimer Declined</p>
</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.