When clicked, update the next TD table

I am trying to create a table that allows users to click on a box and unlock the next column.

Initially, I set it up with two tds and it worked perfectly. However, when I tried to replicate it, the functionality didn't work as expected.

My idea is to have a TD with a specific class that can be clicked. Upon clicking, the JavaScript should enable users to click on the next tds or even the next column, changing their class to clickable like the previous one, and then making the current one non-clickable to prevent clicking again.

This is the code snippet for 2 tds:

            <td>
                <div style="position: relative;" class="can-select1">
                    <img src="http://lkimg.zamimg.com/images/guides/ability-marker.png" style="vertical-align: middle;" class="can-select">
                    <div class="can-select-text">1</div>
                </div>
            </td>
            <td>
                <div style="position: relative;" class="nothing1">
                    <img src="http://lkimg.zamimg.com/images/guides/ability-marker.png" style="vertical-align: middle;" class="nothing">
                    <div class="can-select-text">2</div>
                </div>
            </td>

JavaScript:

$('.can-select1').click(function(e){
    $(this).addClass("is-selected");
    $(this).find('.can-select').addClass("is-selected");
    $(this).children('.can-select-text').addClass("is-selected");
    var nextTd;
    nextTd=$(this).parent().find('.nothing1');
    nextTd.find('.nothing').addClass("can-select1");
    nextTd.addClass("can-select1");
    nextTd.removeClass('nothing1')
});

CSS:

.can-select1 {
    cursor:pointer;
    opacity:0.4;
}
.can-select-text{
    opacity: 0;
    position: absolute; 
    top: 0; 
    left: 0;
    text-align: center;
    width: 32px;
    font: bold 13px/32px 'Trebuchet Ms';
    text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.6); color: #111;
}
.is-selected {
    cursor:default;
    opacity:1.0;
}

.nothing1{
    cursor:default;
    opacity:0;
}

JSFIDDLE for 2 td: http://jsfiddle.net/p3Q7Z/5/

JSFiddle for table: http://jsfiddle.net/p3Q7Z/6/

Answer №1

My focus is not on the initial code you provided, but rather on the subsequent one that is not functioning correctly.

A modification needs to be made in this particular line.

nextTd=$(this).parent().find('.nothing1');

The adjustment involves changing it to:

nextTd=$(this).parent().next().find('.nothing1');

Simply put,

This means that parent() targets the first 'td', while next() targets the second one where you can locate the element with the specified class.

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

Updating the scope in Angular when changing the image source using ng-src is not working

A snippet inside my controller looks like this: $scope.onFileSelect = function($files) { for(var i = 0; i < $files.length; i++) { var file = $files[i]; $scope.upload = $upload.upload({ url: '/smart2/api/files/profi ...

Can Selenium successfully scrape data from this website?

I am currently attempting to extract Hate Symbol data (including the name, symbol type, description, ideology, location, and images) from the GPAHE website using Selenium. As one of my initial steps, I am trying to set the input_element to the XPATH of the ...

Encountering a hiccup while trying to run 'npm install' on WSL2

Whenever I try to run npm install in my project, this error keeps popping up. Oddly enough, I've never encountered this issue on any other machine before. However, this is my first attempt on WSL2 Ubuntu and it's giving me trouble. Here is the er ...

jQuery submission not activating

Within a single HTML file, I have two separate forms that operate independently of each other. The issue I am currently encountering is that the $(form).submit((event)=>{code}) function only works on: <form id="mainForm" action='' ...

A guide on leveraging the power of WebElementPromise in Selenium

Just like falling in love with promises only after the arrival of async/await, my journey with this concept has been a puzzle. /** * WebElementPromise is a promise that will be fulfilled with a WebElement. * This serves as a forward proxy on WebElement, ...

Storing the radio button's selected value in local storage using Vue JS

I have a pair of radio buttons that are linked together to accept a boolean value, either true or false. I am trying to implement a functionality where the selected value is stored in local storage. For example, if true is chosen, then true will be saved i ...

Enhance your slider with tabs using Bootstrap 5

My latest project involved creating a slider using Bootstrap 5. Here is the current result: <head> <title>Bootstrap 5 Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial ...

Is it possible to use jQuery to refresh only a section of the page and modify the URL at the same time?

There is a page (http://myflashpics.com/picture/p9e0) that displays user information along with a small thumbnail on the side. Currently, when clicking on the image, it redirects to a different page and the sidebar reloads as well. I am curious if it' ...

Exporting JavaScript formatting in Netbeans is a breeze

Does anyone know how to preserve the formatting for JavaScript in Netbeans 8.1 when exporting? After clicking on the export button and expanding Formatting, I couldn't find any option specifically for JavaScript. I've thought about locating the ...

Tips for changing the value property of an Input box in Angular JS upon clicking a button

Currently, I am in the process of developing an application utilizing Angular JS. The intention is to have the Restaurant field updated upon clicking the edit button. For instance, when the edit button for Spicy Kitchen is clicked, it should showcase as E ...

I encountered an issue where the variables in my environment files were returning as undefined when I tried to access

After creating my Vue application using vue/cli with the command vue create my-vue, I decided to build the project in next mode. Following the steps outlined in the Vue documentation, I created a .env.next file at the root of the project with the following ...

Is it possible to alter local storage settings?

I've integrated simplecartjs into my online store to manage product data. The data is stored in local storage and currently looks like this: {"SCI-1":{"quantity":1,"id":"SCI-1","price":20,"name":"Matt Black Tape","size":"Empty"},"SCI-3":{"quantity":1 ...

Encountering a SyntaxError with the message 'Unexpected token' while trying to require a module in strict mode from JSON at position 0

Within the index.js file, the following code is present: 'use strict'; const config = require('./config'); In the config.js file, the code looks like this: 'use strict'; const config = new function() { this.port = 3000; ...

Tips for centering a background image:

I have been attempting to set a background image at the center, but all my efforts have failed. Here is the code I have tried: <a href="#" style="background-position:center center;"> <img src="https://firebasestorage.googleapis.com/v0/b/g ...

Building a vanilla JS text sliding feature adorned with interactive arrow buttons

I could really use some guidance on implementing a loop in my code. I have three blocks of text that I want to cycle through using two arrows for navigation. My initial thought was to toggle the "display: none" property for the inactive text blocks. Howev ...

Capturing information within a jQuery function by accessing data from another function

Can data be collected from another function while the function is running? // Custom Function function getData(){ var name = 'tom'; return name } // Main Target Area $('.myDiv').click(function(){ // I need to retrieve dat ...

What's causing the navigation anchor tags to malfunction?

My website consists of two pages. The home page features a one-page navigation system where clicking on 'about' scrolls down to the corresponding section within the same page. PART 1 - On index.html However, when clicking on the 'blog&apo ...

Back from the depths of the .filter method encased within the .forEach

I have been working on this code and trying to figure it out through trial and error: let _fk = this.selectedIaReportDiscussedTopic$ .map((discussionTopic) => {return discussionTopic.fk_surveyanswer}) .forEach((fk) => { ...

What causes the toggle to malfunction when clicked on, specifically when the element is assigned the JS class?

Currently, I am working on animating cards using CSS and JavaScript. Everything seems to be working fine except for one issue – the class does not get removed on the second click on the element when using this.classList.toggle('is-active');. Ca ...

Having trouble with manipulating fabric.js objects within react.js

Currently, I have integrated the fabric.js library into my React application. https://www.npmjs.com/package/fabric In my project, I have successfully added a simplistic yellow rectangle to the canvas using fabric. The yellow rectangle displays correctly ...