Creating a toggle link with 5 different states in coding

I have an HTML table where certain TDs contain inline styles, title tags, and links. I am looking to implement a feature where users can hide these elements by clicking the same icon multiple times. For example, on the first click, only styles are removed, on the second click, only links are removed, on the third click, only title tags are removed, on the fourth click, everything is removed, and on the fifth click, all elements are restored.

Currently, I am able to remove individual elements using the following code snippets:

$('a .icon-remove').click(function () {
    $('.mindteq_content_tbl_container td[style]').css('background-color', '').css('color', '');
});

The same process can be applied to title tags and links as well:

$('.tbl_container td a[title]').remove();

$('.tbl_container td a').removeAttr('href').css('border-bottom', '0px');

However, I am unsure of how to create a button with 5 different states to achieve this functionality.

Answer №1

Instead of removing elements, it's better to simply hide them using the hide() function so you can easily show them again later.

If you're working with a 5-state button, it's important to have an indicator that shows the current state of the button. You can achieve this by using a variable or an aria-attribute.

Example using aria

$(function() {
    $(".toggle").click(function() {
        var $this = $(this);
        var state = parseInt($this.attr('aria-state')) || 0;
        state++;

        $this.text("toggle "+state);
        switch(state) {
            case 5:
                //action 5
                break;
            case 4:
                //action 4
                break;
            case 3:
                //action 3
                break;
            case 2:
                //action 2
                break;
            case 1:
                //action 1
                break;
        }

        if(state >= 5)
            state = 0;

        $this.attr('aria-state', state);
    });    
});

View on JSFiddle

For further enhancement, consider creating a jquery-ui widget and setting up event handlers for the 5 states.

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

How to use jQuery to add an unordered list between two elements

Here is the current structure I am working with: <ul> <li id="first">Fist Item</li> <li id="second">Second Item</li> <li id="third">Third Item</li> .................. </ul> My goal is to add ...

Can a Django form be configured to hide submitted values using display:none?

As I was working on submitting a form in Django with multiple details, I came up with the idea to split it into sections. The first set of details would be hidden initially, and upon clicking the next button, the next set of details would be displayed fo ...

The form's validation fails to recognize dynamically added input after the initial validation

I am currently working on a form that dynamically adds inputs. Whenever the user selects a different "supplier" from the addMaterialSupplier dropdown, a new input for the price is automatically added. The issue I'm facing is that when I click the bu ...

How can I modify the table structure in Ruby on Rails?

Greetings! I am a beginner in the world of Rails and could really use some assistance. Below is the table data extracted from index.html.erb: <table class="table"> <thead> <tr> <th>Area& ...

Stop Code Execution || Lock Screen

Is there a way to address the "challenge" I'm facing? I'm an avid gamer who enjoys customizing my game using JavaScript/jQuery with Greasemonkey/Firefox. There are numerous scripts that alter the DOM and input values. In my custom script, I hav ...

In Vuetify, components are distinct in their own unique way

For some reason, I am facing an issue where a component from Vuetify does not render properly on my localhost compared to the website of Vuetify. Expected result from Vuetify: https://i.sstatic.net/wdPnh.png Actual result from my localhost: https://i.sst ...

Conceal an element along with its space, then signal the application to show alternative content using React

Greetings everyone! I seek assistance with a React Application that I am currently developing. As a newcomer to the Javascript world, I apologize if my inquiry seems trivial. The application comprises of two main elements: a loader, implemented as a React ...

Issue "Invalid UTF-8" encountered while compiling sass using @import

Here is the structure of my project: project assets scripts styles app.scss bower_components node_modules public css app.css js bower.json gulpfile.js package.json I am using gulp-sass to compile app.scss into ap ...

JQuery transmits null values

I have been troubleshooting my code for what feels like forever, but I just can't seem to find the error... My current project involves experimenting with JQuery and AJAX. I created a simple form with a textarea for submission. The form works perfect ...

Customizable form fields in AngularJS

Consider the scenario of the form below: First Name: string Last Name: string Married: checkbox % Display the following once the checkbox is selected % Partner First Name: Partner Last Name: [Submit] How can a form with optional fields be created in Angu ...

Inquire regarding the header image. Can you confirm if the current HTML and CSS for this is the most efficient approach?

This is a preview of my website in progress (Disclaimer: I'm currently learning HTML to build this site, design comes next. I know it's not the conventional way to design a site, but oh well) Check out the site here Is the HTML code for the hea ...

What is the best way to retrieve the current value of an *ngFor loop upon button click?

I have been attempting to retrieve the data.login value of the data item that the user has clicked on the avatar image. Despite my efforts, I have not been able to successfully achieve this through various methods found online. How can I access the current ...

Can I programmatically retrieve a comprehensive list of all global HTML attributes available?

Exploring the extensive list of global HTML attributes can be overwhelming, especially when considering how it continues to evolve with browser updates. Can JavaScript be leveraged to dynamically generate a complete list of these attributes without the ne ...

CSS - Resizing Images

I am experiencing an issue with the width of an image on my website. I want it to span 100% of the browser frame, but it is currently limited to only 1000px wide. Please take a look at: The image in question is located at the top of the page. Is there a ...

Tips for accessing elements with dynamically assigned IDs in jQuery

I'm working on an ASP.Net MVC project and came across the following code: @Html.DropDownListFor(m => m.users, @Model.User, new { id = "UserIds" + @Model.ID }) What is the best way to retrieve the id of this element? ...

Selenium: Mastering the Art of Label Clicking

I'm attempting to interact with this element using Python's Selenium library by employing the following code snippet: driver.find_element_by_('publicForm_customFields').click() Unfortunately, I encountered the following error message: ...

What is the method for using jQuery to retrieve hidden fields with the visible attribute set to "false"?

How can I access a control with "visible = false" attribute using jQuery? Some code examples would be appreciated. <tr>   <td class="TDCaption" style="text-align: left">     <asp:Label ID="lblMsg" runat="server" EnableViewState="False" F ...

An image inside this stylishly framed photo with rounded corners

.a { clip-path: polygon(10% 0, 100% 0%, 100% 100%, 10% 100%,10% 60%, 0% 50%, 10% 40%); } .a { position: relative; width: 500px; height: 500px; background:url("https://cdn.pixabay.com/photo/2020/02/16/07/55/thailand-4852830_960_720.jpg"); border-radi ...

Received a String instead of an Object while attempting to parse JSON data

Currently, my goal is to parse a JSON string using jQuery var parsedData = $.parseJSON('{"graph_info": "{}"}'); I assumed that typeof(parsedData.graph_data) would be an Object but surprisingly it is returning as a string. Can someone guide me o ...

Transfer information to a php file through ajax

I'm facing an issue with receiving data sent via AJAX in a PHP file. The AJAX request seems to be sending data successfully as I receive a success message, but the PHP page shows an error "Notice: Undefined index: ab". Below is my jQuery code for sen ...