SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb accordion menu problem

    Hello guys,
    i'm trying to code an accordion menu with categories for a website!

    Here the HTML:
    Code HTML4Strict:
    <div id="accordion_prodotti">
    	<ul id="accordion">
    		<li><a href="#">Menu 1</a></li>
    		<li><a href="#">Menu 2</a></li>
    		<li><a href="#">Menu 3</a>
    			<ul>
    				<li><a href="#">Item 1</a></li>
    				<li><a href="#">Item 2</a></li>
    				<li><a href="#">Item 3</a></li>
    				<li><a href="#">Item 4</a></li>
    			</ul>
    		</li>
    		<li><a href="#">Menu 4</a>
    			<ul>
    				<li><a href="#">Item 1</a></li>
    				<li><a href="#">Item 2</a></li>
    			</ul>
    		</li>
    		<li><a href="#">Menu 5</a>
    			<ul>
    				<li><a href="#">Item 1</a></li>
    				<li><a href="#">Item 2</a></li>
    				<li><a href="#">Item 3</a></li>
    			</ul>
    		</li>
    	</ul>
    </div>

    Here the jQuery Code:
    Code JavaScript:
    // Accordion
    $('#accordion li').click(function() {
    	if ($('ul', this).is(':visible')) {
    		// Nascondi sottoMenu
    		$("ul", this).stop(true, true).slideUp(200);
    	} else {
    		// Mostra sottoMenu
    		$("ul", this).stop(true, true).delay(50).slideDown(100);
    	}
    });

    Ok, this is what i wrote in my page but the problem is:
    When i click on a "Menu", the "Item" should SlideDown and when i click back on the same "Menu" it should SlideUp! this works correctly, but what, if i click on a "Item" instead of a "Menu"?! The clicked "Menu" SlideUp! What's wrong with it? I just want to SlideUp/Down when i click on a "Menu" and not when i click on a "Item"!

    I have to say that i'm not a good coder in jQuery and probably i missed some classes to do it! but i would like to learn it aswell, reading some books and tutorials!

    Any ideas, tips? I'd appreciate it! Thank you..

  2. #2
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow CSS

    Here the CSS if it needs:

    Code CSS:
    #accordion_prodotti {
    	border: 1px solid black;
    	width: 200px;
    	height: 300px;
    }
     
    #accordion ul {
    	display: none;
    }
     
    #accordion ul li {
    	font-size: 11px;
    }
     
    #accordion a {
    	text-decoration: none;
    	color: black;
    }
     
    #accordion li:hover {
    	font-size: 12px;
    }

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by hackdiablo View Post
    I just want to SlideUp/Down when i click on a "Menu" and not when i click on a "Item"!
    Your selector is selecting ALL of the li elements that are inside of #accordion, including nested ones.

    If you only want direct descendants of #accordion to be affected, then that is the selection that you need to use.
    '#accordion>li'
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation

    Code JavaScript:
    $('#accordion li').click();
    it should select all the <li> into this tag:
    Code HTML4Strict:
    <ul id="accordion">

    ..but why does it select the others <li> into <ul> that haven't an id="accordion"?! it shouldn't happen i think, bacause i'm not selecting them all..

    this is the "fixed" code with the same problem, i mean just less rows:
    Code JavaScript:
    $('#accordion li').click(function () {
    	$('ul', this).toggle();
    });

    I would really happy if someone could help me. Thanks again

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by hackdiablo View Post
    Code JavaScript:
    $('#accordion li').click();

    it should select all the <li> into this tag:
    Code HTML4Strict:
    <ul id="accordion">

    ..but why does it select the others <li>
    Because that's how a CSS selector works.
    If you use a CSS selector to say that all paragraphs in the footer should be green, that affects all of the paragraphs in the footer, no matter how deeply nested they are, not just those that are direct descendants of the footer itself.

    See my previous post about using > to select only direct descendants.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ye, right! that's so strange how jQuery is changing my way to think and act on a site web! Thank you for replying!

  7. #7
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Code JavaScript:
    $('#accordion >li > ul').hide().click(function(e) {
    	e.stopPropagation();
    });
     
    $('#accordion >li').toggle(function() {
    	$(this).find('ul').slideDown();
    }, function() {
    	$(this).find('ul').slideUp();
    });

    That's it, that's all!

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    With '#accordion >li > ul' too, you can simplify that to '#accordion ul' because there are no other UL elements that you need to filter out of that.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •