B.Glock B.Glock - 1 year ago 46
jQuery Question

Show nth:div 1, when i click nth: anchor

Good Evening helpful people of stackoverflow,

I want to hide the **clicked **

and show the appropriate description div

I read through some articles regarding my question, but i run into a logical brickwall in my head. Needlessly to say, i'm a beginner in jquery and maybe there is a better way to do that, i just didn't find it.

Here's what i found so far as "answers":

Hide Children, Show nth Child - the closest answer to my question

Show and Hide Several Links - this solution makes my head dizzy

My HTML consists of two rows of divs, similar to that simplified representation:

<div class="wrapper">
<div class=".project-tile-normal">some pictures</div>
<div class=".project-tile-normal"></div>
<div class=".project-tile-normal"></div>

<div class=".detail-tile">description</div>
<div class=".detail-tile"></div>
<div class=".detail-tile"></div>

This is what i have so far coded:


$('.project-tile-normal').on("click", function() {
if( $(this).hasClass("active") ) {
} else {
var itemid = '#div' + $(this).attr('target'); // my own try to get the Element of the divs.

$('button').on("click", function(){
});//document ready

Should i put all the Items in an array and then count it out? Thanks for your help in advance.

Answer Source

Firstly note that your class attributes in the HTML should not contain any . characters.

With regard to the JS, you can link elements by index by retrieving the index() from the clicked element then selecting the matching required element using eq(), something like this:

$('.project-tile-normal').click(function() {
    var index = $(this).index();

Working example