Solved

jQuery Generalization

Posted on 2016-09-12
3
68 Views
Last Modified: 2016-09-12
I have a nested DIVs which represent a menu structure where the topmost element is displayed and on hover I make each top elements associated DIV visible.  

I have two questions:
How can I implement my jQuery to show/hide the DIVs without a lot of flashing?
How can I generalize the jQuery statement to handle as many top level elements as I add to avoid hardcoding per top level (the all follow the same naming and nesting conventions)?

I will paste the DIVs and then the jQuery I use to process them.

       //Fragment of the jQuery
	$('.top_1').hover(function(){
   			    $(".main_1").show();
				$(".top_2").hide();
				$(".top_3").hide();
				$(".top_4").hide();
			}, function(){
			$(".main_1").hide();
				$(".top_2").show();
				$(".top_3").show();
				$(".top_4").show();
	});				
		
	$('.top_2').hover(function(){
			    $(".main_2").show();
				$(".top_1").hide();
				$(".top_3").hide();
				$(".top_4").hide();
			}, function(){
	  		    $(".main_2").hide();
				$(".top_1").show();
				$(".top_3").show();
				$(".top_4").show();
	});				
	
//etc.  

Open in new window


<!-- Fragment of the DIVS -->
<!-- ***********************************************************************************	-->
<div class="TopMenuLink_1" style="float:left;padding:20">

    <div class="top_1" style="float:left;">
        <a href="http://www.w3schools.com/js/default.asp">1-JavaScript Tutorial</a><br/>
    </div> <!-- Close Top Div	-->
    
    <div class="main_1" style="padding:20;width:auto;">

        <div class="col_1_1" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">2-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">3-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">4-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->
    
        <div class="col_1_2" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">5-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">6-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">7-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->

        <div class="col_1_3" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">8-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">9-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">10-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->

        <div class="col_1_4" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">11-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">12-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">13-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->

        <div class="col_1_5" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">14-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">15-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">16-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->

        <div class="col_1_6" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">17-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">18-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">19-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->

    </div><!-- Close Main Div	-->

</div><!-- Close TopMenuLink Div	-->

<!-- ***********************************************************************************	-->
<div class="TopMenuLink_2" style="float:left;padding:20">

    <div class="top_2" style="float:left;">
        <a href="http://www.w3schools.com/js/default.asp">20-JavaScript Tutorial</a><br/>
    </div> <!-- Close Top Div	-->
    
    <div class="main_2" style="padding:20;width:auto;">

        <div class="col_2_1" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">21-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">22-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">23-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->
    
        <div class="col_2_2" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">24-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">25-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">26-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->

        <div class="col_2_3" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">27-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">28-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">29-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->

        <div class="col_2_4" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">30-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">31-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">32-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->

        <div class="col_2_5" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">33-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">34-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">35-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->

        <div class="col_2_6" style="float:left; height:100px;width:auto;padding-right:5">
            <a href="http://www.w3schools.com/js/default.asp">36-JavaScript Tutorial</a><br/>    
            <a href="http://www.w3schools.com/html/default.asp">37-HTML Tutorial</a><br/>    
            <a href="http://www.w3schools.com/js/default.asp">38-JavaScript Tutorial</a><br/>
        </div><!-- Close Col Div	-->

    </div><!-- Close Main Div	-->

</div><!-- Close TopMenuLink Div	-->

<!-- etc. -->

Open in new window

0
Comment
Question by:Howard Bash
  • 2
3 Comments
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41794136
1. Give all your top <div>'s a generic class  of main-menu (remove the top_1, top_2 etc)
2. Add this jQuery
jQuery
$(function() {
  $('.main-menu').hover(function() {
    $(this).addClass('active');
  }, function() {
    $(this).removeClass('active');
  });
});

Open in new window

Add this CSS
.main-menu + div {
  display: none;
}
.main-menu.active + div {
  display: block;
}

Open in new window

Working sample here
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 41794163
Nice!!!! Thanks.
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41794512
You are welcome.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Find out what you should include to make the best professional email signature for your organization.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now