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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:42:12+00:00 2026-05-10T21:42:12+00:00

i have the following jquery code. basically i will have several overlapped divs and

  • 0

i have the following jquery code.

basically i will have several overlapped divs and a list of links on the right of all those overlapped divs. when hovering over a link, the link’s assigned div will fade in.

I have the following code and it works (it uses the default windows’ sample pictures), but if someone can think of a way to optimize it or make it generic, i’d appreciate it.

<html> <head>     <script type='text/javascript' src='jquery-1.2.6.min.js'></script>      <script type='text/javascript'>        $(document).ready(function() {            $('#trigger1').mouseover(function() {              $('.contentdiv').addClass('inactive');             $('#divsunset').removeClass('inactive');             $('.inactive').fadeOut(500);             $('#divsunset').fadeIn(500);           });             $('#trigger2').mouseover(function() {              $('.contentdiv').addClass('inactive');             $('#divwinter').removeClass('inactive');             $('.inactive').fadeOut(500);             $('#divwinter').fadeIn(500);            });            $('#trigger3').mouseover(function() {              $('.contentdiv').addClass('inactive');             $('#divbh').removeClass('inactive');             $('.inactive').fadeOut(500);             $('#divbh').fadeIn(500);            });            $('#trigger4').mouseover(function() {              $('.contentdiv').addClass('inactive');             $('#divwl').removeClass('inactive');             $('.inactive').fadeOut(500);             $('#divwl').fadeIn(500);            });          });      </script>       <style>         #divsunset         {           position: absolute;           top: 5px;           left: 5px;         }         #divwinter         {           position: absolute;           top: 5px;           left: 5px;         }         #divbh         {           position: absolute;           top: 5px;           left: 5px;         }         #divwl         {           position: absolute;           top: 5px;           left: 5px;         }         #links         {           position: absolute;           top: 800px;           left: 700px;         }         .inactive         {         }     </style> </head> <body>  <div id='divsunset' class='contentdiv'>     <img src='Sunset.jpg' /> </div>  <div id='divwinter' class='contentdiv'>     <img src='Winter.jpg' /> </div>  <div id='divbh' class='contentdiv'>     <img src='bh.jpg' /> </div>   <div id='divwl' class='contentdiv'>     <img src='wl.jpg' /> </div>    <br /> <div id='links'>     <a href='#' id='trigger1'>Show Sunset</a>     <a href='#' id='trigger2'>Show Winter</a>     <a href='#' id='trigger3'>Show Blue Hills</a>     <a href='#' id='trigger4'>Show Waterlillies</a> </div>  </body> </html> 

Thank you Matt, TM and kRON, your responses really helped.

I dont feel I explained myself totally, but TM provided the answer I was looking for.

I wanted to follow DRY and the code TM provided helped me best this time since it did not require for me to alter my markup, just the jQuery code.

Again, thanks a lot. Its amazing how quickly I got the answer. Keep up the great work.

  • 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. 2026-05-10T21:42:12+00:00Added an answer on May 10, 2026 at 9:42 pm

    The first thing you can do, to make it cleaner is replace all those similar calls with something more generic.

    (note: assume all of this is inside of document.ready())

    $('#trigger1').data('target', '#divsunset'); $('#trigger2').data('target', '#divwinter'); $('#trigger3').data('target', '#divbh'); $('#trigger4').data('target', '#divwl'); $('#trigger1,#trigger2,#trigger3,#trigger4').mouseover(function() {     var selector = $(this).data('target');     $('.contentdiv').addClass('inactive');     $(selector).removeClass('inactive');     $('.inactive').fadeOut(500);     $(selector).fadeIn(500); }); 

    Using comma-delimited selectors is a great way to obey DRY with jQuery.

    I use $(element).data() to set and retrieve a some data on the element, in this case, the selector used to update the appropriate element.

    Also, just for a cleaner visual look, you can use the following in the place of $(document).ready(), if you prefer it. It’s the exact same thing, just a different syntax.

    $(function() {    //DOM ready }; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 64k
  • Answers 64k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer ASP.NET Localization using Resource Files Examples: Globalization and Localization in… May 11, 2026 at 10:53 am
  • added an answer From what I have found from extensive search on my… May 11, 2026 at 10:53 am
  • added an answer You could draw several offscreen images where you gradually fill… May 11, 2026 at 10:53 am

Related Questions

i have the following jquery code. basically i will have several overlapped divs and
I have the following JQuery code that worked perfect in C#/Asp.net 2.0 to call
I have the following JQuery code which does similar functionality like Stackoverflow where the
Let's say I have the following jQuery AJAX call: $.ajax({ type: POST, url: MyUrl,
I am begging to use jquery. I have the following call that works in
How do you rotate an image using jQuery-rotate plugin? I have tried the following
I have the following tables in my database that have a many-to-many relationship, which
I have the following web config file. I am having some difficulty in retrieving
I have the following script. It replaces all instances of @lookFor with @replaceWith in
I have the following code in a web.config file of the default IIS site.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.