If the element contains a specific class, then remove that class and apply a new

Encountering an issue here. I have 4 HTML elements structured like this:

    <!-- Light Color Scheme - Header False and Show Faces True -->
        <div class="fb-like-box h-f_dsf-t visible" data-href="http://www.facebook.com/pages/Alpine-Adaptive-Scholarship-Program/344942928870040?fref=ts" data-width="292" data-show-faces="true" data-stream="true" data-header="false" colorscheme="light"></div>

Although they are in various variations. For instance, one could have header true and show faces true with a unique class ".h-t_dsf-t". My goal is for the JavaScript to determine which one is active and which one isn't, allowing only one element to have the .visible class at a time while others have the .invisible class.

Thank you in advance!

My JavaScript focuses on the Show Faces Button(currently, .sf-no.click doesn't contain any content yet, but it will in the future. Right now, I'm focusing on getting it to work for the yes button first.)

// Show Faces
$(document).ready(function() {
    $('.sf-yes').click(
        function() {
            $('.sf-yes').removeClass('btn-not-active btn-active').addClass('btn-active');
            $('.sf-no').removeClass('btn-not-active btn-active').addClass('btn-not-active');
            // Show Faces Yes &&& Show Header No
            if ($('.sf-yes.sh-no').hasClass('.btn-active')) {
                $('.h-f_dsf-t').removeClass('.invisible').addClass('.visible');
                $('.visible').removeClass('.visible').addClass('.invisible');
            }
            // Show Faces Yes &&& Show Header Yes
            if ($('.sf-yes.sh-yes').hasClass('.btn-active')) {
                $('.visible').removeClass('.visible').addClass('.invisible');
                $('.h-t_dsf-t').removeClass('.invisible').addClass('.visible');
            }
            // Show Faces No &&& Show Header Yes
            if ($('.sf-no.sh-yes').hasClass('.btn-active')) {
                $('.visible').removeClass('.visible').addClass('.invisible');
                $('.h-t_dsf-f').removeClass('.invisible').addClass('.visible');
            }
            // Show Faces No &&& Show Header No
            if ($('.sf-no.sh-no').hasClass('.btn-active')) {
                $('.visible').removeClass('.visible').addClass('.invisible');
                $('.h-f_dsf-f').removeClass('.invisible').addClass('.visible');
            }
        }
    );
    $('.sf-no').click(
        function() {
            $('.sf-no').removeClass('btn-not-active btn-active').addClass('btn-active');
            $('.sf-yes').removeClass('btn-not-active btn-active').addClass('btn-not-active');
        }
    );
});

Answer №1

.find('.active-button')

must be

.find('active-button')

excluding the period .

Answer №2

Your code contains a few issues that need to be addressed. When adding or removing classes, avoid including the full stop symbol "." before the class name. This mistake is present in instances of .hasClass, removeClass, and addClass.

Furthermore, your conditional statements appear to be conflicting. It seems like you may have intended for elements with classes .sf-yes and .sh-no to be separate, but your jQuery statement is looking for an element with both classes.

Additionally, it's important to note that using unique IDs like #sf-yes is preferred over class selectors. If multiple class selectors are necessary, consider assigning them to variables at the beginning of your function call.

Lastly, it's advisable not to categorize this as a Facebook question since your code pertains specifically to jQuery and not Facebook-related functionalities.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When Select2 doesn't find a suitable option, the text "other" will be displayed

Is it possible to show a default option in select2 dropdown if the user's typed input does not match any available options? $("something").select2({ formatNoMatches: function(term) { //return a predefined search choice } }); I have searched ...

The functionality of if and else statements within local storage is not functioning as

I've been working on implementing a styleswitcher for my website. I successfully created a dropdown menu and saved it in localstorage. However, I'm facing an issue when trying to use the localstorage information to trigger an alert() through if a ...

What steps do I need to take to get a website up and running with the index.html

After successfully hosting my website, I encountered an issue where it opens the index of the website (a list of files added via FTP) instead of the Index.html page. Can you advise on how to resolve this? Your assistance is greatly appreciated. ...

Guide to crafting a flawless pixel-perfect separator with CSS borders

I have a menu with a black background and a submenu with a grey background. Now, I want to add a pixel-perfect divider between them. However, I'm unsure of which border color to use in order to achieve this perfection. Can anyone provide some guidan ...

Obtain the script's event feedback from the WebView and transfer it to React Native

My HTML script is used to display a module, but I encountered an issue with the eventHandler not responding. How can I ensure that the eventHandler in Web View triggers responses? I am not receiving any logs for the onSuccess, onError, or onClose methods w ...

Implementing setTimeout with the copy button: A guide

How can I implement a setTimeout function in the copy button so that when a user clicks on it, the text will change to "copied" and then revert back to "copy" after 3-4 seconds? Please help me find a solution to this problem and also optimize the JavaScrip ...

Selecting text from a specific row using jQuery

Here is a table that displays information about different programming languages: <table id="languages" border="0" cellspacing="1"> <thead> <tr> <th>Language</th> <th>Type</th> <th>Inv ...

PHP's 'include' function is now being ported into modern Javascript as a new method

As the library of JS frameworks continues to expand, I'm wondering if there is a simple JS replacement or alternative for PHP's 'include' function. Is PHP include still a relevant method for including chunks of code, or are there better ...

What is the best way to input a dynamic post array in PHP?

Explaining this might be challenging, but I will do my best. I have multiple input posts with custom conditions, which means there can be as many custom conditions as needed. Here is the HTML: <input name="type[]" value="type 1"> <input nam ...

What methods can be utilized to ensure that my wistia video player/playlist is responsive on different devices

I need some assistance with making my Wistia player responsive while using a playlist. I want the video player to adjust its size based on the screen size. Here is an example: My current code utilizes their iframe implementation: <div id="wistia_1n649 ...

What are some effective methods for organizing data to optimize user interface functionality?

In my current project, I am developing an application that involves displaying multiple items on a single page. Each item contains a wealth of information that is dynamically generated in the code behind (.aspx.cs) and then shown on the user interface. As ...

Looping through Jquery Ajax requests

Currently, I am developing a SQL converter that converts MySQL to MongoDB. The user interface for this converter is being created with the help of Ajax. Ajax is crucial for handling large conversions. Due to the limitations of MySQL select code, the conve ...

The Stylebook in Delphi 10 Seattle does not apply correctly on forms other than the Main form

Within my application's MainForm, I have 3 Stylebooks available for users to choose from. Once a selection is made, the same Stylebook is applied to other Forms within the program. While most of the styles are properly set, there is one toolbar that s ...

What is the best way to select the enclosed <a> tag within an <li> element?

Below is the JavaScript code I have attempted: $('#footer').find('.browse li').click(function(e){ $(this).find('a').click(); }); Here is the relevant HTML: <div id="footer" class="span-24"><div ...

Ways to stop CKEDITOR from automatically saving textarea or contenteditable content

I've integrated the CKEDITOR plugin for a format toolbar feature on my web application. It seems that the message shown above is a default one provided by CKEDITOR. My goal is to have users start with a blank textarea every time they visit the page, ...

When utilizing jQuery post to send data to a node.js server, the request is abruptly terminated before completion

When sending a post request to my node.js server, I use the following code: $.post("/admin/google",{result: result, testName: testName}, function(data){ console.log(data); if(data.status === 200) { alert ...

What could be the reason for my difficulties in retrieving the necessary data from this website with Selenium?

Basically, I've been struggling to interact with the "enter your address" field on a particular website through xpath and selenium. I'm curious as to why this is happening. Detailed Explanation The website in question is this one. It features a ...

Ensuring Uniform Column Height in Bootstrap 4 - Preventing Buttons from Being Forced to the Bottom of

Using the Bootstrap 4 class 'h-100' to ensure equal height for all three columns, I encountered an issue where the buttons that were initially aligned to the bottom of each div are no longer in correct alignment. How can I maintain button alignme ...

Master the art of creating click events using only CSS

Is it possible to achieve this example using only CSS? If not, what would be the alternative solution? Any JSFiddle examples would be greatly appreciated! How can I also add slashes - should I use an image or can it be done with CSS? And what about the t ...

The substance (singular word) extends beyond the margins of the mobile screen

I am facing an issue on my webpage where the title is too long and it goes out of the page on mobile devices. Here is an example: <div class="module-head"><h3 class="module-head-title">Search Engine Optimization - Why Is It ...