Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4017268
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:50:26+00:00 2026-05-20T09:50:26+00:00

jQuery(document).ready(function(){ $(function() { $(‘.button-a’).each(function() { $(this).hover( function () { $(‘.button-title’, this).animate({ opacity: 0.6 },

  • 0
jQuery(document).ready(function(){
  $(function() {
    $('.button-a').each(function() {
      $(this).hover(
        function () {
          $('.button-title', this).animate({ opacity: 0.6 }, 200);
        });
      });
   });
$(function() {
  $('.button-b').each(function() {
    $(this).hover(
      function () {
        $('.button-title', this).animate({ opacity: 0.6 }, 200);
      });
    });
  });
$(function() {
  $('.button-c').each(function() {
    $(this).hover(
      function () {
        $('.button-title', this).animate({ opacity: 0.6 }, 200);
      });
    });
  });
$(function() {
  $('.button-d').each(function() {
    $(this).hover(
      function () {
        $('.button-title', this).animate({ opacity: 0.6 }, 200);
      });
    });
  });
});

I have many buttons, when hover each, there will have some opacity change.

The codes are similar, the small different is $('.button-a'),$('.button-b'),$('.button-c'),$('.button-d'), whether can combine jquery each function? so that I can shorter my code? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:50:26+00:00Added an answer on May 20, 2026 at 9:50 am

    Two simplifications:

    var fn = function() {
       $(this).hover(
           function () {
              $('.button-title', this).animate({ opacity: 0.6 }, 200);
           }
       });
    });
    
    jQuery(document).ready(function() {
        $('.button-a').each(fn);
        $('.button-b').each(fn);
        $('.button-c').each(fn);
        $('.button-d').each(fn);
    });
    

    You can shorten the on-ready call too:

    var fn = function() {
       $(this).hover(
           function () {
              $('.button-title', this).animate({ opacity: 0.6 }, 200);
           }
       });
    });
    
    $(function() {
        $('.button-a').each(fn);
        $('.button-b').each(fn);
        $('.button-c').each(fn);
        $('.button-d').each(fn);
    });
    

    And you can merge selectors:

    var fn = function() {
       $(this).hover(
           function () {
              $('.button-title', this).animate({ opacity: 0.6 }, 200);
           }
       });
    });
    
    $(function() {
       $('.button-a, .button-b, .button-c, .button-d').each(fn);
    });
    

    Now, you don’t really need .each, as .hover will automatically apply to each element given by the selector:

    $(function() {
       $('.button-a, .button-b, .button-c, .button-d').hover(function () {
           $('.button-title', this).animate({ opacity: 0.6 }, 200);
       });
    });
    

    Take your pick!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On document ready I run this code: jQuery(document).ready(function(){ jQuery('#button').click(function() { jQuery('#contact_form').load(/Users/mge/Downloads/jquery-ajax-1/readme.txt); return false; });
I have this: jQuery(document).ready(function(){ jQuery('a.color2030').click( function() { jQuery('tbody').hide(); jQuery('tbody.color2030').show(); }); jQuery('a.color2031').click( function() { jQuery('tbody').hide();
I have a working script like this: jQuery(document).ready(function(){ $('.video-thumb img').bind('mouseover',function(){ var new = $(this).attr('src').replace(/default.jpg/,'1.jpg');
I wrote this function: jQuery(document).ready(function() { jQuery('input[type=text]').each( function(i) { thisval = jQuery(this).val(); jQuery(this).blur( function()
The problem behind my question in the title is this: jQuery( document ).ready( function()
Jquery has a great language construct that looks like this: $(document).ready(function() { $(a).click(function() {
I have created a function outside of $(document).ready() , but am still using jQuery
Why does this: jQuery(document).ready(function($) { console.log(test); }); produce this error message: console is not
I have the following function: jQuery(document).ready(function($){ $(div.findButton).click(function(){ UidID=Math.floor(Math.random()*1000); PostCode=$(#postcode).val(); $.get(result.php, {postcode: PostCode,uid:UidID}, function(response){ $(#result).html(response);
This is my jquery code jQuery(document).ready (function() { // post $('.post').bind('mouseenter mouseleave', function() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.