java
sql
iphone
css
ajax
xcode
android
ruby-on-rails
mysql
objective-c
multithreading
json
facebook
cocoa
tsql
apache
mvc
asp
postgresql
dom
You need to use a common naming convention for the div and the links like so:
div
<ul id="bannerList" class="list"> <li>Type <ul> <li><a href="#" id="link-1">Flash banner1 120x600</a></li> </ul> </li> <li>Other Type <ul> <li><a href="#" id="link-2">Flash banner2 120x600</a></li> </ul> </li> </ul> <div id="bannerTarget"> <div class="vert" id="div-1">alpha</div> <div class="horiz" id="div-2">one</div> <div class="horiz" id="div-3">two</div> <div class="horiz" id="div-4">three</div> <div class="horiz" id="div-5">four</div> </div>
Your code then becomes:
$("ul#bannerList li a").click(function(e) { e.preventDefault(); $('#bannerTarget > div').hide(); var id = $(this).attr('id').replace('link-',''); $('#div-'+id).show(); });
You can also achieve the same without using ID's but you'll need to keep the link order and the div order the same:
$("ul#bannerList li a").click(function(e) { e.preventDefault(); $('#bannerTarget > div').hide(); var link_index = $(this).parent().index(); $('#bannerTarget > div').each(function(){ if( $(this).index() == link_index ){ $(this).show(); } }); });