I am trying to do a simple tab navigation.
I was thinking I could put all buttons in an object, do the same with the content divs, get index of clicked button and access the content div using that index number to make the respective content show up. What am I doing wrong?
HTML:
<div class="row">
<a class="btn" href="#">One</a>
<a class="btn" href="#">Two</a>
<a class="btn" href="#">Three</a>
<a class="btn" href="#">Four</a>
</div>
<div class="row">
<div class="content">Content 1</div>
<div class="content">Content 2</div>
<div class="content">Content 3</div>
<div class="content">Content 4</div>
</div>
jQuery:
$(document).ready(function(){
var $btn = $('.btn');
var $content = $('.content');
$btn.each(function(){
$(this).on('click', function(){
$content.hide();
var i = $btn.index(this);
$content[i].show(); //This does not work
});
});
});
jsFiddle: http://jsfiddle.net/pcr0zuuo/
Thanks in advance!