Because of the CSS tree structure, it is not possible to add or remove classes. This restriction applies to all actions done in jQuery, including click

I want to create a hover effect where the CSS changes when hovering over an item, then reverts back to normal when no longer hovered. Additionally, I would like the CSS to change when the item is selected, and return to normal when another item in the same line is selected.

HTML

 <div class="body2">
                    <div class="block" id='block'>
                        <div class="block-header">
                            <div class="arrow-down" style="left:83px"></div>

                        </div>
                    <div class="block-body">
                        <p style="margin-top:25px;"></p>
                        </div>
                        </div>
                        <div class="block" id='block'>
                        <div class="block-header">
                            <div class="arrow-down" style="left:284px"></div>

                        </div>
                    <div class="block-body">

                        </div>
                        </div>
                    <div class="block" id='block'>
                        <div class="block-header">
                            <div class="arrow-down" style="left:485px"></div>
                        </div>
                    <div class="block-body">

                        </div>
                        </div>
                        <div class="block" id='block' style="margin-right:0px;">
                        <div class="block-header">
                            <div class="arrow-down" style="left:686px"></div>

                        </div>
                    <div class="block-body">

                        </div>
                        </div>
                    </div>

CSS

.body2 .block{
display:inline-block;
border:solid 2px #eaeaea;
height: 165px;
width: 180px;
margin-right: 19px;
}
.body2 .block:hover{
cursor:pointer
}
.body2 .block .block-header{
    height: 35px;
border-bottom: solid 2px #eaeaea;
width: 180px;
}
.body2 .block .block-header .arrow-down{
 width: 15px;
height: 15px;
border: 2px solid #eaeaea;
transform: rotate(45deg);
border-top: 0;
border-left: 0;
position: absolute;
left: 10%;
background-color: #fff;
z-index: 10;
top: 74px;
 }

.body2 .block .block-body{
width:174px;
 }

JS

$('.body2 #block').hover(function(e){
    $($(e.currentTarget).find('.block-header')).css('border-color','blue');
    $($(e.currentTarget).find('.block-header')).css('background-color','blue');
    $($(e.currentTarget).find('.arrow-down')).css('background-color','blue');
    $($(e.currentTarget).find('.arrow-down')).css('border-color','blue');
    $(e.currentTarget).css('border-color','blue');
}, function(){
    $('.body2 .block .block-header').css('border-color','blue');
    $('.body2 .block .block-header').css('background-color','blue');
    $('.body2 .block .block-header .arrow-down').css('background-color','blue');
    $('.body2 .block .block-header .arrow-down').css('border-color','blue');
    $('.body2 .block').css('border-color','blue');
} );

Although the hover effect functions correctly on my local machine, I am having trouble implementing the click selection. Here is a JSFiddle demonstrating the issue.

Thank you

Answer №1

Check out my solution on this fiddle: https://jsfiddle.net/op4fLwbo/15/

Here's the updated javascript:

$('.body2 .block').hover(function() {
    $(this).find('.block-header').toggleClass('hovered');
    $(this).find('.arrow-down').toggleClass('hovered');
    $(this).toggleClass('border-hover');
});

I made sure to include jquery in the code, which was causing the issue on fiddle.

In the css, I added !important to the new classes for better specificity, but you may need to adjust accordingly.

I hope this resolves your problem and meets your requirements.

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

Send a bunch of checkbox values to the controller upon clicking in order to apply a filter to a query using

How can I filter the number of students by campus, year, and group using checkboxes? I have successfully filtered the result by Campus using this code, but the uncheck functionality is not working as expected. It still sends the checked value. I need to ...

"Enhance search functionality by integrating live search results with clickable hyperlinks

I am currently working on implementing a live search feature in Laravel 7. Everything is functioning correctly, however, I am facing an issue where I am unable to add a (href a tag) to the result list. The results are being displayed in a select option usi ...

Utilizing CSS to incorporate a background image into HTML code

Is it feasible to utilize pure HTML for the background-image property? Consider the following scenario: The original: background-image:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' version='1.1' height=&apo ...

How can you retrieve the ID of the container at the top layer using jQuery?

My webpage is filled with HTML controls that look like this: <span id="pic1" class="container"> <span class="inner"> <span class="img"></span> <span class="strip"></span> <span class="remove_ ...

Leverage the power of JavaScript validation combined with jQuery

Hello, I'm relatively new to the world of Javascript and jQuery. My goal is to create a suggestion box that opens when a user clicks on a specific button or div element. This box should only be visible to logged-in users. I have some knowledge on how ...

Setting a random number as an id in the constructor in Next JS can be achieved by generating a

What steps can be taken to resolve the error message displayed below? Error: The text content does not match the HTML rendered by the server. For more information, visit: https://nextjs.org/docs/messages/react-hydration-error Provided below is the code i ...

The dynamic drop-down menu is giving incorrect values when the onchange function is called

Trying to implement Google Analytics tracking on my dynamic dropdown menu in WordPress has been a bit tricky. I want to be able to track when users click on any of the options and display the name of the selected value, not just the ID. However, I've ...

Searching for a different method in JavaScript that can add items without duplication, as the prependTo function tends to insert multiple items

Every time my code runs successfully, a success message is generated and displayed using prependTo within the HTML. However, the issue arises when the user performs the successful action twice, resulting in two success messages being shown on the screen. ...

Fade effects on hover with a changing background color

Here is the CSS code to be used: #onec-fourth:hover { background-color: #F36DE1; color: white; } I am looking to keep the background color and text color effects for 1 second after I move my mouse off the object (#onec-fourth). Currently, when I mov ...

Executing pure JavaScript code in Grails using Groovy

this is a duplicate of Executing groovy statements in JavaScript sources in Grails with a slight variation, my intention is to only render the js-code without enclosing it in script tags picture someone loading a script from my server within their html l ...

What is the proper way to utilize a filtered object based on its properties within an Angular controller?

Currently, I am working on developing a stacked graph along with its associated table. To retrieve the necessary JSON data, I am utilizing $http.get() and then assigning it to $scope.dataset. Here's a snippet of the HTML: <input ng-model="_search ...

Is it possible to create an index for an associative array based on a string in JavaScript?

Within my JavaScript code, I am working with an associative (two-dimensional) array (myObj[x][y]). Each row in this array contains a different number of elements denoted by 'n', where the first value signifies the amount 'n' as shown be ...

struggling with beginner-level JavaScript issues

Hey there, I've been running into a bit of an issue with my basic javascript skills: function tank(){ this.width = 50; this.length = 70; } function Person(job) { this.job = job; this.married = true; } var tank1 = tank(); console.log( ...

What happens when you click on paper-tabs in a polymer?

Struggling to get click events to fire on <paper-tabs> and <paper-tab>. Interestingly, when manually adding event listeners in Chrome's developer tools, it works fine. But the same code doesn't seem to work in my application: // app. ...

Ways to show text on top of an image using CSS

Seeking help! I'm attempting to overlay some text on top of an image, here's the link: http://jsfiddle.net/5AUMA/31/ The challenge is aligning this text to the bottom part of the image. I've tried using this CSS: .head_img p { position ...

Bootstrap 4 and Angular 7 application experiencing issues with toggle button functionality in the navigation bar

I am currently working with Angular7 and Bootstrap4. I am attempting to integrate a navbar into my application, but I am facing an issue where the toggle button does not work when the navbar is collapsed. It is important for me to mention that I do not wa ...

Innovative CSS trick for styling checkboxes alongside non-related content

The CSS checkbox hack demonstrated below assumes that the content is a sibling of the checkbox. By clicking on the label, the content will toggle. Check out the DEMO here <input id="checkbox" type="checkbox" /> <label for="checkbox"> Toggle ...

How to Sort Multiple Tables in jQuery Datatables

Imagine a page filled with multiple tables, all of which are identical in structure (although the data may vary). Now, picture this: what if we could simply click on a column header in one table and have all the tables automatically sort based on that colu ...

The Intex XDK is experiencing difficulties with establishing an Ajax connection

Encountering an issue with connecting to a php page through Ajax on my phone. I've simplified the process as much as possible. Here is the server-side code: <?php echo(json_encode('success')); ?> And here is the client-side code: ...

Regular expressions can be used to remove specific parts of a URL that come before the domain name

Looking for some help with a javascript task involving regex and the split function. Here's what I need to achieve: Input: http://www.google.com/some-page.html Output: http://www.google.com I attempted the following code but unfortunately it didn&ap ...