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

  • SEARCH
  • Home
  • 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

Related Questions

I have the following jquery code which doesn't work quite right when trying to
I have the following code: jQuery('#hexVal').change(function() { var hexvarval = jQuery('#hexVal').val(); jQuery('#thumbnail_hover_color').val(hexvarval); }); Basically,
I have the following code for the jQuery validation plugin.... basically on submit, I
i have the following jquery code. basically - it takes a value from an
i have the following jQuery code. $(function() { $('.clickme').click(function() { $('.totalthing').slideToggle('slow','easeInOutQuart', function() { //
I have the following jquery code: $(document).ready(function() { $(body[class*=page-calendar-practices] #content-header h1#title).after(<div id='athletics_practice-schedule'><div id='inner-title'><a href='www.example.com'
I have the following jQuery code: $(body.page-calendar-practices-2012-05 #content-header h1#title) which works fine. I can
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following jQuery code which runs when I'm clicking an option in
I have the following JQuery code: <script type=text/javascript> $(document).ready(function () { var $containerHeight =

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.