1

I am writing a Laravel 5 project with a comment section code below

@foreach($Comment as $Comment)
  <div id="comment-{!! $Comment->comments_id !!}" class="comment-wrapper">

     <div class="btn btn-lg btn-info btn-xs" class="show">Show</div>
  <div class="btn btn-lg btn-success btn-xs" class="hide">Hide</div>
  <div class="btn btn-lg btn-warning btn-xs" class="toggle">Toggle</div>

      <div class="watch" class="jumbotron alert-info">



         <ul class="list-group">
           <li class="list-group-item list-group-item-success">{!! $Comment->author  !!}</li>
           <li class="list-group-item"> {!! $Comment->text !!}</li>
           </ul>
             @if ($Comment->author == Auth::user()->name)
               <p><a href="{!! URL::route('deleteComment', $Comment->comments_id) !!}"  class=" btn-danger btn-xs" style="float:right">Delete</a></p>  



              @endif

        <h6><small>CREATED ON: {!! $Comment->created_at !!}</small></h6>
</div>
</div>
          @endforeach

and I have a javascript file which looks like this

 $(document).ready(function () {

 $('.show').click(function () {
    $(this).closest('.comment-parent').find('.watch').show('slow');
});
 $('.hide').click(function () {
    $(this).closest('.comment-parent').find('.watch').hide('slow');
});
 $('.toggle').click(function () {
    $(this).closest('.comment-parent').find('.watch').toggle('slow');
});

});

The trouble is the toggle/hide javascript function only works on one set of buttons and hides all of the comments. I want to have the set of buttons that work for each comment individually. I've tried to increment the watch class and buttons div id by adding 1 and incrementing it for each comment but can't get it to work. Any help would be appreciated thanks.

Mhluzi Bhaka
  • 1,364
  • 3
  • 19
  • 42
Paul Haskett
  • 21
  • 1
  • 5
  • Why do you have a `
    ` tag inside an `
      `?
    – haakym Jun 11 '15 at 18:44
  • The `id` attribute for a element in html should be unique, you have the same value in a foreach loop, assign it to a class instead or give it a name with an integer next to it. – haakym Jun 11 '15 at 18:47
  • for styling is that not good? – Paul Haskett Jun 11 '15 at 18:51
  • I believe it's invalid HTML, you also have a `` and `` tag that pop out of nowhere. I reckon you probably want to go other you code and then run it through a html validator. Apologies if this comes across harsh, not trying to be annoying! – haakym Jun 11 '15 at 18:54
  • ok thanks that's what I was trying to do but must have got the syntax wrong. what would be the correct way of doing that? – Paul Haskett Jun 11 '15 at 18:54
  • not at all mate I'm quite new at this and any criticism is constructive. Thanks – Paul Haskett Jun 11 '15 at 18:56
  • You only want to nest `
  • `s in `
      `s or `
      `s. `
      `s can be nested in `
      `s I believe. Awesome, I wish you all the best with your learning - that's the best attitude to have! And as for `id` attribute for html elements you only want them to have unique values for a given page.
  • – haakym Jun 11 '15 at 19:01
  • 1
    I've edited my first post with updated html changes. – Paul Haskett Jun 11 '15 at 20:05