John Davidson

php - Jquery toggleClass for mulitiple buttons

0 comments
Message:


The next button loops information coming from the database using PHP so I have multiple buttons. When I hover over the image, the button with the view-data class, will toggle the class d-block, but I want it to show only the next button, not all the buttons together. How can I do it?




$(document).ready(function() {
$(".product-img").hover(function() {
$(".view_data").toggleClass("d-block");
});
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="product-img" style="width:100%;" />
<input type="button" name="view" value="Quick View" class="view_data">


My Answer:
To show only the next button when hovering over the image, you can use the `next()` method in jQuery to target the next element with the class `view_data`. Here's how you can modify your code:

javascript
$(document).ready(function() {
$(".product-img").hover(function() {
$(this).next(".view_data").toggleClass("d-block");
});
});


In this code, `$(this)` refers to the current `.product-img` element that is being hovered over. The `next(".view_data")` method targets the next element with the class `view_data` after the `.product-img` element. This way, only the next button will toggle the class `d-block` when hovering over the image.

Rate this post

5 of 5 based on 8053 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us