0

I have this menu:

enter image description here

What I want to do: when I click the image button on the right (#sub-menu) I want it to open the sub-menu (.sports2).

this is a sub-item html code for an example:

                <a href="#"><li> Golf  
                    <img src="strokesmenu.png"  id="sub-menu" />
                    <ul class="sports2">
                        <a href="#" class="selected"><li>British Open</li></a>
                        <a href="#" class="selected"><li>Masters</li></a>
                        <a href="#" class="selected"><li>PGA Championship</li></a>
                        <a href="#" class="selected"><li>US Open</li></a>
                    </ul>
                </li></a>

Why this code isn't working for me?

$('#sub-menu').click(function(){
    //$('.sports2').slideToggle("slow");
    $(this).find('ul>li').slideToggle(slow);
})
4
  • 2
    Your Markup is Invalid a tags can't be direct childs of the ul element. Commented Dec 2, 2014 at 14:03
  • Also, seems you are using #sub-menu multiple times, which is invalid. Commented Dec 2, 2014 at 14:04
  • 1
    Also find will search inside the element ... but the submenu isn't inside the img of course, so you need to use next() or another method Commented Dec 2, 2014 at 14:08
  • .next() will not work according to HTML it's wrong HTML structure. Look at source code. Commented Dec 2, 2014 at 14:26

1 Answer 1

2

first of all, <a href="#" class="selected"><li>British Open</li></a> this structuring is so wrong I cannot even describe it how wrong it is.

convert it to the <li><a href="#" class="selected">British Open</a></li> if you want to make click-able while li try using below css or similar to that

ul li a { display: inline-block; width:100%; height:100%; }

also there must be only one item with one id having multiple item is against the W3C rules and clicking the little icon is not so user friendly. so instead give class to main item li and hanle the click with that one.

$('li.main').click(function(){
    $(this).find('ul').slideToggle('slow');
})

apparently you cannot do that so you have to bind it to the img first change id to the class e.g. class="sub-menu"

$('li img.sub-menu').click(function(){
    //$(this) -> img .next() -> ul
    $(this).next().slideToggle('slow');
})

now the $(this).find('ul>li').slideToggle('slow'); should work but it will open every li and might cause some problem issues.

instead I suggest using $(this).find('ul').slideToggle('slow'); so the list can be opened/closed. you see the animation differences by trying it and choose the best one for you.

EDIT FOR CLICK BUG: well not sure if I get it right but as I understand in some cases you need to redirect the page in others open the sub menu. in that case you can check if the li has submenu or not the following code should do the trick.

$('li.main').click(function(){
    if ($(this).has("ul")) // if has submenu
        $(this).find('ul>li').slideToggle('slow');
     else
        // your redirect code.
})
Sign up to request clarification or add additional context in comments.

11 Comments

What you need is $(this).next('.sports2').slideToggle('slow');. this is the image.
yeah just notice the problem and updated my answer accordingly.
Now is ok, but you still need slideToggle('slow') unless slow is a variable.
what does "main" stand for?
main li is the li item Golf text in it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.