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?
 
Gurvinder Pal SinghCommented:
$(".link").hover(function(){
   $(this).find(".link").show();
},
function(){
   $(this).find(".link").hide();
});
0
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.