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 6657577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:47:07+00:00 2026-05-26T01:47:07+00:00

I was wondering if anyone has any idea on how I could rewrite this

  • 0

I was wondering if anyone has any idea on how I could rewrite this simple jquery code to be more efficient. It’s of course working fine now but I imagine adding say 10 more items would make the code really big. I thought maybe I could add the classes to arrays and use some kind of loop? Not sure if that’s the right approach though.

Here it is on jsfiddle:
http://jsfiddle.net/QVS9X/42/

and here’s a sample of it:
JS:

$(".image1").mouseout(function() {
    $(".default").show();
    $(".cat1").hide();
}).mouseover(function() {
    $(".default").hide();
    $(".cat1").show();
});

$(".image2").mouseout(function() {
    $(".default").show();
    $(".cat2").hide();
}).mouseover(function() {
    $(".default").hide();
    $(".cat2").show();
});

HTML:

<div class="image1 image">
    <p>Hover for cat 1</p>
</div>
<div class="image2 image">
    <p>Hover for cat 2</p>
</div>
<div class="image3 image">
    <p>Hover for cat 3</p>
</div>
<div class="default">
    <p>Default Text</p>
</div>
<div id="cats">
    <p class="cat1">Category 1 text</p>
    <p class="cat2">Category 2 text</p>
    <p class="cat3">Category 3 text</p>
</div>
  • 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-26T01:47:07+00:00Added an answer on May 26, 2026 at 1:47 am

    If you can put a class of image on the divs that current have image1, image2 etc, then you can do this:

    $(".image").hover(function() {
        $(".default").toggle();
        $("#cats p").eq($(this).index()).toggle();
    }); 
    

    This assumes that the image divs will be in the same order as the p tags inside #cats.

    http://jsfiddle.net/QVS9X/44/

    Example using data attributes:

    JS:

    $(".image").hover(function() {
        $(".default").toggle();
        $($(this).data('id')).toggle()    
    });  
    

    HTML:

    <div class="image" data-id="#cat1">
        <p>Hover for cat 1</p>
    </div>
    <div id="cats">
        <p id="cat1">Category 1 text</p>
    </div>
    

    http://jsfiddle.net/QVS9X/55/

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

Sidebar

Related Questions

Just wondering if anyone has any idea how I can Prerender Animations in jQuery.
I am getting this error, and am wondering if anyone has any idea how
Basically I'm wondering if anyone has any tips for ensuring your code is well
Just wondering if anyone has any idea of why my filter order is being
Wondering if anyone has any idea where the hardware buttons are programmed in the
I'm wondering if anyone has any idea on how to implement a projection painting
I am wondering if anyone has any idea how to make the controls stack
I am wondering if anyone has any experience using a JQuery plugin that converts
Just wondering if anyone has any ideas on how we can style templates... Like
I am wondering if anyone has any real world numbers on performance differences between

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.