0

I have a WordPress site where I have some "pipe" elements which I want to change color when clicked. I'm new to jQuery so I would need some help to do this. This is what I have so far (a very simplified version of it all):

jQuery:

jQuery(function() {
  jQuery(".--clearGreen").click(function() {
    jQuery(this).addClass("--clearGreenClicked");
  });
});

CSS:

.--clearGreen {
 background-color: rgba(255, 255, 255, 0);
 border: 2px solid green;
}

.--clearGreenClicked {
 background-color: blue;
 border: 2px solid blue;
 color: white;
}

This works to add the .--clearGreenClicked class to the clicked .--clearGreen "pipe" that I have on the page. What I would like to achieve is to make it so that only the clicked "pipe" gets the change applied to it and all the other "pipes" return to the original styling, so basically that they "switch" styles depending on what "pipe" is clicked. I've tried to use .not in combination with .has but I can't seem to get it to work properly. Thanks!

6
  • use toggle for class Commented Feb 19, 2019 at 11:54
  • can you post a fiddle ? Commented Feb 19, 2019 at 11:57
  • 1
    Your wording does not match your code. "this works to add the class to all the pipes" - nope, it only does it to the one you clicked, which is then what you're asking. If you can create an minimal reproducible example (snippet) you might be able to explicitly demonstrate the issue to us. Commented Feb 19, 2019 at 11:59
  • @freedomn-m absolutely Commented Feb 19, 2019 at 12:00
  • @freedomn-m I think your answer meets what the OP wants ;) Commented Feb 19, 2019 at 12:01

3 Answers 3

3

As worded:

only the clicked pipe gets the change and the other pipes [remain] with the original styling"

your code does exactly that, because you've used $(this).addClass.

So assuming you're asking "and the other pipes return to the original styling", so only one is "selected" at a time:

jQuery(function() {
  jQuery(".--clearGreen").click(function() {
    jQuery(".--clearGreenClicked").removeClass("--clearGreenClicked");
    jQuery(this).addClass("--clearGreenClicked");
  });
});

Additional:

You can use .not to reduce the changes made, eg:

jQuery(".--clearGreenClicked").not(this).removeClass("--clearGreenClicked");
jQuery(this).addClass("--clearGreenClicked");

which will stop the code from removing and then re-adding the class - this could make a difference if you have a transition on the css.

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

2 Comments

Hmm yeah, I should have formatted it better, but this fixes my issue. And it was this simple well I got some to learn, but many thanks for your answer!
Glad it helps. It's not always easy to describe an issue and, especially in development, precise wording can make a difference.
1
<script type="text/javascript">
    jQuery(function () {
        jQuery(".--clearGreen").click(function () {
            jQuery(this).toggleClass("--clearGreenClicked");
        });
    });
</script>

Comments

0

Use toggleClass function to remove class when it exist and add class if it doesn't exist. Like this:

<script type="text/javascript">
    jQuery(function () {
        jQuery('.--clearGreen').click(function () {
            jQuery(this).toggleClass('--clearGreenClicked');
        });
    });
</script>

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.