1

Just starting using css and jQuery and I have a CSS class called .content which I'm using for a side of the page navigation box, picture of the day box, statistics box, etc., which are layed out using a definition lists.

So I need it when I click on the <dt> tag it slides up/down the <ul> tag underneath it which is simple enough. but how do I determine which which instance of the .content class was clicked on... I know seperate id's would work but was wondering if there is a quicker way? Hope I explained my problem clear enough any help would be greatly appricated =]

Thanks in advance!

2
  • 1
    A little HTML example would really help... Commented Sep 6, 2010 at 11:19
  • "<dt> tag it slides up/down the <ul>" Shouldn't that be dd? Commented Sep 6, 2010 at 16:07

2 Answers 2

3

When you're inside a click handler (or any event handler), this refers to the current element, so you would do this:

$(".content").click(function() {
  $(this).find('ul').slideToggle();
  //or possibly..
  $(this).next('ul').slideToggle();
});

Where $(".content") inside would again select all of them (sounds like your current issue), this refers to the DOM element that was clicked, so you can just do whatever action you want to it specifically.

Sign up to request clarification or add additional context in comments.

3 Comments

Yeah i did try that mate but my problem is i need something like ` $(".content").click(function() { $(this + "ul").dosomething(); });` Which doesnt work for me :<
@Shiverz - I updated the answer to show how this is done, if it's a child, use .find(), or it's a sibling, use .next() :)
@Shiverz - welcome :) and welcome to SO!, be sure to accept answers on this and future questions, it'll make future ones more appealing to would-be answerers :)
0

Specify using $(this) within your function for the selected class. That will then use the correct class clicked on. For example:

$('.content').click(function() {
   $(this).hide();
});

This is obviously in it's simplest form and you can expand from there.

Comments

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.