0

I'm trying to make a simple dropdown menu, this is my code so far:
HTML

<div id="menuBox">
    <ul>
        <li>
           <a href="" class="activate">item 1</a>
           <ul class="subs" style="display: none;">
            <li>
                <a href="">Sub 1 </a>
                <a href="">Sub 2 </a>
            </li>
          </ul>
        </li>
        <li>
           <a href="" class="activate">item 1</a>
           <ul class="subs" style="display: none;">
            <li>
                <a href="">Sub 1 </a>
                <a href="">Sub 2 </a>
            </li>
           </ul>
        </li>
    </ul>
</div>

JQUERY

jQuery(".activate").hover(function(){
    jQuery(this).find('.subs').css("display", "block");
}, function(){
    jQuery(this).find('.subs').css("display", "none");
});

When I try it on my site nothing happens, what am I doing wrong?

2
  • 1
    Please make JSFiddle for better understanding Commented Oct 16, 2014 at 9:55
  • fix this.. href="class="activate"" should be href="" class="activate" Commented Oct 16, 2014 at 9:58

3 Answers 3

8

You currently have no elements with an .activate class because your HTML is messed up. Change:

<a href="class="activate"">item 1</a>

To:

<a class="activate">item 1</a>

Once you've fixed that, you're still going to have problems as .subs isn't a descendant of .activate. You'll need to use jQuery's next() method to select the .subs element instead:

jQuery(".activate").hover(function(){
    jQuery(this).next('.subs').css("display", "block");
}), function(){
    jQuery(this).next('.subs').css("display", "none");
});
Sign up to request clarification or add additional context in comments.

2 Comments

It was correct in my code, just copied it wrong here. And your solution worked, I will accept it when I can
@user3164891 please see the part of my answer about fixing your jQuery.
0

try

HTML

<div id="menuBox">
    <ul>
        <li>
           <a href="" class="activate">item 1</a>
           <ul class="subs" style="display: none;">
            <li>
                <a href="">Sub 1 </a>
                <a href="">Sub 2 </a>
            </li>
          </ul>
        </li>
        <li>
           <a href="" class="activate">item 1</a>
           <ul class="subs" style="display: none;">
            <li>
                <a href="">Sub 1 </a>
                <a href="">Sub 2 </a>
            </li>
           </ul>
        </li>
    </ul>
</div>

JS

jQuery(".activate").hover(function(){
    jQuery(this).next('.subs').show();
}, function(){
    jQuery(this).next('.subs').hide();
});

DEMO

Comments

-1
<a href="class="activate"">item 1</a>

Corrected above line as:

<p class="activate" style="cursor:pointer;">item 1</p>

Comments

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.