0

I have a html structure like this:

<div class="container">
  <dt class="odd">...</dt>
  <dd class="odd">...</dd>
  <dt class="even">...</dt>
  <dd class="even">...</dd>
  <dt class="odd">...</dt>
  <dd class="odd">...</dd>
  <dt class="even">...</dt>
  <dd class="even">...</dd>
</div>

the sturcture above have dt with class odd and dt with class even, what i want is when i clik the dt with class odd the next dd with class odd will show or hide, but the problem is my on click function for dd is not firing at all, here's the code:

$( "dt.odd" ).click(function() {
   alert('odd');
   $(this).parent().next('dd.odd').show();
});
8
  • Check console for errors.. Have you included jQuery ? Code looks fine and working for me.. Commented May 26, 2017 at 5:57
  • no error at all, i already included the jquery, when i change the selector for click function to only $('dt').click without odd it firing the click function Commented May 26, 2017 at 6:02
  • Are you adding elements dynamically ? Commented May 26, 2017 at 6:03
  • @Rayon no, i'm using php loop to generate this html Commented May 26, 2017 at 6:04
  • what is the default status of the div, are they display none or block? Commented May 26, 2017 at 6:04

3 Answers 3

1

try this, may be this will help u

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style>
dt{
	display:block;
}
dd{
	display:none;
}
</style>
<script>
$(document).ready(function(){
$( "dt.odd" ).click(function() {
    $(this).next().show();
});
});
</script>
</head>
<body>
<div class="container">
  <dt class="odd">...</dt>
  <dd class="odd">...</dd>
  <dt class="even">...</dt>
  <dd class="even">...</dd>
  <dt class="odd">...</dt>
  <dd class="odd">...</dd>
  <dt class="even">...</dt>
  <dd class="even">...</dd>
</div>
</body>
</html>

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

1 Comment

@GaneshPutra thanks, after wrapping it inside document ready function it works
0
$( "dt.odd" ).click(function() {
   alert('odd');
   $(this).next().show();
});

Comments

0

If you want to show and hide respective dd on click of dt then you can use below snippet :

$( "dt.odd" ).click(function() {
  $(this).next().toggle();
});

When you click on dt it will show next dd, if not visible and vice-versa

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.