Hide/Expand menu with javascript

Hi experts,

I have a html menu that is automatically generated by a Java object and I don’t have any control over what it produces. The menu which it outputs is included below.

I need this menu to expand in an accordion like manner with javascript. The links entitled 'Primary Link' should always be present but their sublinks entitled 'Secondary link' should only be visible when you hover on the primary/parent link.

Does anyone know how i might go about doing this without having to alter the html?

Any help would be greatly appreciated.

Looking forward to hearing from you.
<ul>

<li class="link_selected"><a href="cms.jsp?menu_id=5638" >Primary Link 1</a></li>
<li class="link"><a href="cms.jsp?menu_id=6313" >Primary Link 2</a>
<ul>
<li class="link"><a href="cms.jsp?menu_id=6314" >Secondary Link 1</a></li>
<li class="link"><a href="cms.jsp?menu_id=5644" >Secondary Link 2</a></li>
<li class="link"><a href="cms.jsp?menu_id=5642" >Secondary Link 3</a></li>
<li class="link"><a href="cms.jsp?menu_id=6542" >Secondary Link 4</a></li>
<li class="link"><a href="cms.jsp?menu_id=6486" >Secondary Link 5</a></li>
<li class="link"><a href="cms.jsp?menu_id=5641" >Secondary Link 6</a></li>
</ul>
</li>
<li class="link"><a href="cms.jsp?menu_id=6488" >Primary Link 3</a>
<ul>
<li class="link"><a href="cms.jsp?menu_id=6493" >Secondary Link 6</a></li>
<li class="link"><a href="cms.jsp?menu_id=6494" >Secondary Link 7</a></li>
<li class="link"><a href="cms.jsp?menu_id=6495" >Secondary Link 8</a></li>
<li class="link"><a href="cms.jsp?menu_id=6496" >Secondary Link 9</a></li>
<li class="link"><a href="cms.jsp?menu_id=6497" >Secondary Link 10</a></li>
<li class="link"><a href="cms.jsp?menu_id=6498" >Secondary Link 11</a></li>
<li class="link"><a href="cms.jsp?menu_id=6492" >Secondary Link 12</a></li>
</ul>
</li>
<li class="link"><a href="cms.jsp?menu_id=6543" >Primary Link 4</a></li>
<li class="link"><a href="cms.jsp?menu_id=6489" >Primary Link 5</a></li>

</ul>

Open in new window

b24Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gurvinder Pal SinghCommented:
$(".link").hover(function(){
   $(this).find(".link").show();
},
function(){
   $(this).find(".link").hide();
});
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Gurvinder Pal SinghCommented:
and off-course the secondary links will hidden first, isn't it?
0
b24Author Commented:
But the primary and secondary links all have the same class="link" so how would it know which ones to hide and expand on hover?

The primary links should always be visible and the secondary links should only be visible when you hover on the primary link to which they belong.

Thanks for your help!
0
Gurvinder Pal SinghCommented:
doesn't matter. which ever link is hovered only that link's children will be shown, if they exists
You can assign another class to the secondary links to make them invisible earlier.
0
b24Author Commented:
Thanks. I'll give it a try.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.