0

I programmatically build a three-level set of DIVs.

Category A
(Title1)
(Content)
(Title2)
(Content)
Category B
Category C

By default, I set levels 2 and below (titles and content) to display:none. With this code, I show all titles belonging to the category clicked:

    $("#catA").on("click", function() {
        $("#catA .itemTitle").css("display", "block");
    });
    ...

Now what I want to do is to show the content just for the title clicked. That seems to have exceeded my knowledge of selectors! Any ideas?

1
  • 1
    Can you show your html. Commented May 10, 2013 at 12:11

2 Answers 2

1

You can try to bind the click event to the .itemTitle and show the next content of it, (If you are using jQuery then no need to use display block to show the hidden element simply you can use show() method):

$('.itemTitle').on('click', function(){
  $(this).next('.content').show();
});  //---------^^^^^^^^-------------my assumption of class change to yours
Sign up to request clarification or add additional context in comments.

2 Comments

yes! this is just an assumption as OP has not posted any html markup and it looks like he has itemTitle div first then content div.
By default, I set levels 2 and below (titles and content) to display:none. this line made me to assume something like this.
0

Try this:

$(".itemTitle").on("click", function() {
        $(".itemTitle >.content").css("display", "block");
    });

1 Comment

Thank you, Somnath. With my interpretation of Jai's suggestion, here is what now works: $("#catA").on("click", function() { $("#catA .itemTitle").css("display", "block"); $("#catA .itemTitle").on("click", function() { $(this).next(".itemContent").css("display", "block"); }); }); Is that what you intend, as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.