Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have few div (may be 4 or 5) , and <ul><li>
C#
<ul><li><a href="#GetCol">Color</a></li></ul>
<ul><li><a href="#GetBrd">Brand</a></li></ul>
<ul><li><a href="#GetMast">Master</a></li></ul>
<ul><li><a href="#GetCars">Cards</a></li></ul>

HTML
<div id="MainAl" class=".allclass">
<div id="GetCol">
Color - Red
</div>
<div id="GetMast">
Master - MySelf
</div>
</div>



I want to show div GetCol and hide other all , when Color(GetCol) list clicked.
same way for GetBrd .. etc....

I hope u understood

Pls advice

regards
Posted
Updated 18-Dec-13 23:43pm
v2

Try this:
C#
$('ul li a').click(function() {
    var href = $(this).attr('href');
    $(href).show();
    $('div#MainAl div:not(' + href + ')').hide();
});

How this works: when a link inside a list is clicked, you get the href attribute from the clicked link. This href attribute is actually the selector for the div that you want to show. So, show this div, and hide all other divs inside div#MainAl.

Online demo: http://jsfiddle.net/ProgramFOX/sGKsW/[^]
 
Share this answer
 
Comments
Peter Leow 19-Dec-13 6:43am    
+5.
Thomas Daniels 19-Dec-13 7:08am    
Thank you!
To add to the solution by ProgramFOX, suggest adding 'event.preventDefault()':

JavaScript
$('ul li a').click(function(event) {
    event.preventDefault();
    var href = $(this).attr('href');
    ...


See document:event.preventDefault()
 
Share this answer
 
v2
Comments
maulikshah1990 19-Dec-13 6:43am    
ok..thanks..but i got solution using sibling() ....
Peter Leow 19-Dec-13 6:45am    
Well done.
maulikshah1990 19-Dec-13 6:56am    
but when i come to page again, i get to see all div and one after the other..how to show only one div and hide others , when i come to page first time
Thomas Daniels 19-Dec-13 7:22am    
+5, it's indeed a good idea to avoid 'jumping' on the page.
Give your target elements class or id then bind click events to it.
http://api.jquery.com/click/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900