Target specifically the onhover div element using JQuery and trigger the smooth sliding down effect with the SlideDown() function

Is it possible to create a unique JQuery slidedown() function that will only be applied to the div where the mouse is hovering?

I have managed to make it work by giving each div a separate class, but when I name them all the same, it triggers the slide down effect on all divs instead of just the one being hovered over.

I believe there must be a simpler solution that I am overlooking.

My code can be seen in this FIDDLE

HTML

<div class="q1">1. Question (on hover)<br/>
<div class="desc1">Description: Here goes the question one from the php variable</div>
<br/></div>
<div class="q2" >2. Question (on hover)  <br/>
<div class="desc2">Description: Here goes the question two from the php variable</div>
<br/></div><div class="q3">3. Question (on hover)<br/><div class="desc3">Description: Here goes the question two from the php variable and so on</div><br/></div>

JQUERY

jQuery(function () {
    for (var i = 0; i < 100; ++i) {
        (function (i) {
            jQuery('.desc' + i).hide();
            jQuery('.q' + i).hover(
                function () {
                    jQuery('.desc' + i, this).stop(true, true).delay(300).slideDown(300);
                },
                function () {
                    jQuery('.desc' + i, this).stop(true, true).slideUp(200);            
                });
         }(i));
    }
});

CSS

.desc1,.desc2,.desc3 {
    font-size: 12px;
    font-style: italic;
}

.q1,.q2,.q3 {
    font-weight:bold;
}

This method works well for a small number of questions, but what if there are many more? My current approach would require creating CSS styling for every single div, which is not practical. There must be a better way to achieve this. Is there a simple solution or perhaps a more complex one?

Answer №1

The Violin: http://jsfiddle.net/PLGz5/8/

The Code:

jQuery(".description").hide();

jQuery(".question").hover(function(){
    jQuery(this).find(".description").stop().slideDown("slow");
}, function(){
    jQuery(this).find(".description").stop().slideUp("slow");
});

Answer №2

Here is a potential solution that could be helpful for your situation:

*Latest Demo Fiddle

If you are uncertain about the number of questions or if they will be added or removed dynamically, consider delegating events from a common parent element (such as the document). Make sure to use consistent classes for all the questions.

UPDATE - To prevent any confusion, I have revised this code by adding a wrapping element to avoid directly binding to the document. Check it out below:

HTML:

<div class="question-wrapper">
    <div class="question">1. Question (reveal on hover)<br/>
    <div class="description">Description: This is question one fetched from a PHP variable</div>
    <br/></div>
    <div class="question">2. Question (reveal on hover)<br/>
    <div class="description">Description: This is question two fetched from a PHP variable</div>
    <br/></div>
    <div class="question">3. Question (reveal on hover)<br/>
    <div class="description">Description: This is question three and so forth from a PHP variable</div>
    <br/></div>
</div>

JS:

$('.question-wrapper').on('mouseenter mouseleave','.question', function(e){
    if (e.type === 'mouseenter'){
        $(this).find('.description').stop(true, true).slideDown();
    }
    else {
        $(this).find('.description').slideUp();
    }
});

Answer №3

If you take a look at your code, you'll notice a parameter called 'this' which will indicate the divs where the mouse cursor is located. This functionality seems to be working well as demonstrated in the example below.

A simple adjustment to your code (such as modifying the CSS class names and jQuery elements) should resolve any issues.

Check out the JSFIDDLE demo here

Here's the HTML:

<div class="q">1. Question (on hover)<br/>
<div class="desc">Description: This is question one taken from a PHP variable</div>
<br/></div>
<div class="q">2. Question (on hover)<br/>
<div class="desc">Description: This is question two retrieved from a PHP variable</div>
<br/></div>
<div class="q">3. Question (on hover)<br/>
<div class="desc">Description: This represents question three obtained from a PHP variable and so forth</div>
<br/></div>

jQuery part:

jQuery(function () {
  for (var i = 0; i < 100; ++i) {
    (function (i) {
        jQuery('.desc').hide(); 
        jQuery('.q').hover(
            function () {
                jQuery('.desc', this).stop(true, true).delay(300).slideDown(300);
            },
            function () {
                jQuery('.desc', this).stop(true, true).slideUp(200);            
            });
     }(i));
  }});

CSS styling:

.desc {
    font-size: 12px;
    font-style: italic;
}

.q {
    font-weight:bold;
}

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

Arrange items in a single parent Div in flex-column format, aligning them to the left and

In my Angular application, I am experimenting with stacking Bootstrap toast elements in a customized vertical layout. Specifically, I want certain toast elements to be positioned on the left side of the page (using the align-items-start style) and others o ...

What is the process for incorporating a CSS class into a preexisting Woocommerce/Wordpress widget?

Looking for a way to add additional classes to the Woocommerce Product Categories Widget without tampering with the original source code or creating a replacement widget? The widget_display_callback seems like the way to go, but the available documentation ...

Are Half of the <a> Links Invisible due to a Possible Issue with Position:Fixed?

There seems to be an issue with some of the links in my navigation bar not working properly. It appears that they are being covered by an iframe element, preventing them from changing color when clicked. Although the iframe element is visibly below the na ...

Utilize Selenium to extract information from a webpage, including content that is dynamically generated through JavaScript

Currently, I am facing a dilemma: my desire is to extract information from a webpage (for example, this one) regarding the apps that are available, and then store this data into a database. In my quest to achieve this task, I have opted to use crawler4j t ...

Unable to display the value of my array in JSON encoded PHP due to it being undefined

In my cart.php file, I have encoded an array to JSON and returned it to an AJAX function: $data = array(); $data['total'] = '10000'; $data['quantity'] = '10'; echo json_encode($data); In my index.php f ...

Creating a seamless navigation experience using Material UI's react Button and react-router-dom Link

Is there a way to have the Material UI react Button component behave like a Link component from react-router-dom while preserving its original style? Essentially, how can I change the route on click? import Button from '@material-ui/core/Button' ...

Integrate JQuery-Ui into an Angular 7 application using an external .js file

I'm currently working on an Angular 7 project and facing some challenges while trying to integrate the JQuery-Ui plugin. I have successfully installed both JQuery and the plugin, and added them to the scripts array in my angular.json file. Even though ...

The function addClass() seems to be malfunctioning

I'm currently experimenting with creating a scrolling cursor effect on a string of text. The goal is to make it look like the text has been highlighted with a blinking cursor, similar to what you see in your browser's search bar. window.setInter ...

Passing Down Instance Methods Using Static References in JavaScript/TypeScript

✋ Exploring the concept of access modifiers from TypeScript, how can we make it relevant for JavaScript developers as well? Let's consider a scenario where a parent class defines necessary members and a shared method: // ParentClass.js export defaul ...

Ways to customize the default countdown timer

I came across this amazing project at https://codepen.io/mattlitzinger/pen/ysowF. While the project is wonderful, I am looking to make some modifications to the code, specifically targeting a specific date. Here is the JavaScript code snippet: var tar ...

What could be causing the issue with uglify not functioning properly with AngularJS content?

I've created some gulp tasks to assist in building my web project. One of the tasks involves minifying js files. Here is the task code snippet: gulp.task('minify' , function() { console.log('Copy minified js '); return gulp ...

What steps do I need to take in order to generate a legitimate link annotation within Adobe Acrobat by utilizing Acrobat

Seeking guidance on how to embed an Acrobat Javascript within Adobe Acrobat in order to generate a link annotation. The current method involves using the "addLink" function within the document object, which triggers a Javascript action upon clicking the li ...

Verify the Absence of an Internet Connection Through a Popup on a Windows 10 Application Developed in Javascript

Hey there, I've been browsing the web but can't seem to find a comprehensive tutorial on how to write a code that displays an error message when there is no internet connection. I'm currently using Visual Studio to develop a Windows 10 App w ...

Page rotates on hover effect with RotateY

How can I get an image to rotate on the Y axis when hovered over? My code works in -moz- but not in -webkit- or -o-. What am I missing? .spin-logo { height: 450px; margin: 0 auto; -moz-transition: transform 2000ms ease 0s; -o-animation: transfor ...

What is the best way to retrieve a selected value from one dropdown list and populate it into another dropdown

Can someone assist me with retrieving the selected answer from one drop-down list and populating it into another drop-down list? Below is my code for programming groups A and B: Example: If a user selects an option from group A and group B, I would li ...

Receiving a 405 error when making an API call - could the routing be misconfigured? (Using NextJS and Typescript)

Hey there, I've been working on implementing a custom email signup form that interfaces with the Beehiiv newsletter API. If you're interested, you can take a look at their API documentation here: Beehiiv API Docs Currently, my web application i ...

Having trouble with the placeholder blur feature on images in Next.js?

Within my website, I have a dynamic route set up as /titles/[slug].js. Upon initially navigating to this route, everything functions as expected - the placeholder blur effect displays on all images and animations triggered by react-intersection-observer wo ...

What is the best way to activate materialise pop-up menus in a React application?

Looking to enhance my React app by adding a NavBar with dropdown functionality. Here's the code snippet: <ul id="dropdown1" className="dropdown-content"> <li><NavLink to="/create">Create lesson ...

Using inline SVG within a Vue.js component

Recently, I embarked on a Vuejs project using @vue/cli version 3.0.0-beta.16. In my Home.vue single file component, I encountered an issue while trying to import and add inline SVG to the template. The crux of the problem lies in the fact that vue cli alr ...

After the function has been executed, the default parameters will still be present. What should be done in this

When I set default parameters in a function and then call the function again without those parameters, they still remain. I want them to be reset on every function call. It seems like a simple issue, but as a beginner, I'm struggling to understand it ...