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

How can I apply the "active" class to a standard header?

source: header.php <?php $currentPage = basename($_SERVER['PHP_SELF']); echo $currentPage; ?> <div class="nav-links"> <nav class="navbar navbar-inverse"> <div class="container"> <div c ...

How can a loading circle be displayed upon clicking a button on a PHP website using JavaScript?

As a newcomer to the world of JavaScript programming, I'm facing a challenge that seems deceptively simple. My goal is to display a loading circle when a user clicks on an upload button, trigger external PHP code for image processing, and then make th ...

SVG is not extending to the entire viewport in mobile view

I need help with adjusting wave SVG's on my website to fill the entire viewport or screen width. Does anyone have any suggestions? To create the waves, I used a Wave SVG generator and placed them within a div element. <nav> <div> //align ...

Ways to access JSON data in JavaScript

I'm experiencing difficulty accessing the JSON data provided below. My approach involves utilizing the JavaScript AJAX success function, and when attempting alerts with the following code, $.ajax({ type:'GET', url:myURL, success : function ...

Creating a web form with HTML and integrating it with jQuery AJAX

Looking to fetch data from the server through jQuery AJAX on an HTML form and store the response in a PHP string variable. Here is my current code snippet: <form method="post" name="myform" id="myform" action="https://domain.com/cgi-bin/cgi.exe"> &l ...

Is there a way to deactivate a button upon clicking and then substitute it with a new button that serves a different purpose in AngularJS?

Is there a way to deactivate a button once clicked and substitute it with another button that performs a different task using AngularJS? Here is the button in question: <button type="submit" class="btn btn-small btn-default" ng-disabled="isteam==0 || ...

Motion, animated image while the script is being executed

I've developed a script named notify_me.php which I execute in the following manner: script type="text/javascript"> //Running notify_me.php script $(document).ready(function(){ $("#submit").click(function(){ var d = $(&ap ...

I am curious to see the number of people who have made their selection

I am currently using JavaScript to alter the text value from "select" to "selected". My next step is to include the selected text in a cart. Can you please provide some guidance? Thank you in advance. PHP CODE : <a class='btn slct' href=&ap ...

Searching for elements in Selenium using JavaScript's equivalent of findElement(by.className)

I need to locate an id tag within a class tag on an html page. driver.find_element(By.XPATH, "//div[@class='Content warning']/p").get_attribute("id") https://i.stack.imgur.com/NlU8t.png I successfully accomplished this with ...

Is it possible to apply a style change to several components at once using a single toggle switch

I am looking to implement a day/night feature on my web app, triggered by a simple toggle click. While I can easily add this feature to a single component using the navigation menu, I am faced with the challenge of incorporating it into multiple component ...

Python Selenium fails to retrieve table data

I am encountering an issue where the print statement at the end of my code returns NONE. I aim to store values at every 6th spot in each row, which is functioning correctly. However, when I attempt to print(self.mystr) at the end, it does not give me a val ...

What could be the reason for the history.length being 2 on the initial page?

Why is it that when I open a new browser window and load a page in it, the history.length property is always 2 in both Chrome and Firefox? You can test this phenomenon yourself by visiting the following link: http://jsbin.com/amiyaw ...

Class Div is visible only within the jQuery Tab

I have encountered a perplexing issue that has left me stumped. I recently added tabs using a jQuery plugin [Tabulous] and am in the process of transferring existing content into these tabs. However, I stumbled upon a strange problem while attempting to po ...

What is the best way to determine if a text input is empty or not?

Can someone explain how to check if a text input is empty or not in a jQuery statement? Is the following code correct? $('#generate').click(function(){ if($('#lastname').val() === ''){ alert("Field cannot be empty!") ...

initiate a page refresh upon selecting a particular checkbox

So I've got this code that saves around 30 checkbox selections to local storage, which is working fine. Then there's this separate checkbox below, when checked it automatically reloads the page: <input type="checkbox" id="autoload" class="aut ...

Removing or hiding elements using jQuery

My HTML code includes a select tag, and when the value changes, I want to retrieve data from the database based on this new value. The data is then displayed in a new div. This new div contains a close sign that should hide the div when clicked by the user ...

Creating a sleek animation with JQuery for a dynamic-width div

Here is a snippet from my latest project. Take a look at the demonstrationFIDDLE. In this project, I have created a list with buttons: <a href="#f1" class="bt">1 <div class="show">Computers Networking</div> </a> When you ...

JQuery functions for click and hover are not functioning as expected on a div element

I am having trouble with a div that I have declared. The click event is not working as expected, and I also need to use the :hover event in the css, but it isn't functioning either. What could be causing this issue? <div id="info-button" class="in ...

Conceal a button using an AJAX POST request

I'm encountering an issue with my Ajax post where I am trying to disable the button used to submit data. I've reviewed my code and it seems accurate, but the button is not getting disabled. I attempted using $("#refreshButton").attr("disabled", t ...

How can I completely alter the content of aaData in jquery.dataTables?

Looking to completely change the content of a datatable purely from a javascript perspective, without relying on Ajax calls. I've attempted using the following script: oTable.fnClearTable(); oTable.fnAddData(R); oTable.fnAdjustColumnSizin ...