0

Using codeigniter 3.x

i'm trying to get data from database using foreach loop.

<h3 style="margin-right:15px;" id='hideshow'>August 2016</h3>
<?php foreach($duxeos as $e): ?>
<div class='content' ><h4 class="dropdate"><?php echo $e->fulldate;?></h4><div class="cdropdate" class="defhide"><?php echo $e->content;?></div></div>
<?php endforeach; ?>

javascript :

  jQuery(document).ready(function(){
      jQuery('.hideshow').live('click', function(event) {
           jQuery('.content').toggle('show');
      });
  });

  jQuery(document).ready(function(){
      jQuery('.dropdate').live('click', function(event) {
           jQuery('.cdropdate').toggle('show');
      });
  });

now it's working, but when i press the hide button, it hide all content, how can i hide content that i want ?

8
  • I do not see any loop in your code.. Also not that, There must not be multiple elements in a document that have the same id value. Commented Aug 9, 2016 at 4:04
  • foreach($data as $e) is foreach loop Commented Aug 9, 2016 at 4:07
  • All right! Consider second point in my comment.. Commented Aug 9, 2016 at 4:08
  • yeah i know that, if it didn't allow multiple elements in same id, then how can it change the id value on every loop ? Commented Aug 9, 2016 at 4:14
  • Consider using class instead of id, it will make your life easy.. Commented Aug 9, 2016 at 4:15

1 Answer 1

2
  • Use this context in handler-function
  • Use .on instead of .live
  • Use .closest to get the closest element in order to find child of it.

jQuery(document).ready(function() {
  jQuery('.dropdate').on('click', function(event) {
    jQuery(this).closest('.content').find('.cdropdate').toggle();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<h3 style="margin-right:15px;" id='hideshow'>August 2016</h3>
<div class='content'>
  <h4 class="dropdate">Full-Date</h4>
  <div class="cdropdate defhide">Content</div>
</div>
<hr>
<div class='content'>
  <h4 class="dropdate">Full-Date</h4>
  <div class="cdropdate defhide">Content</div>
</div>

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

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.