Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How to Display and Hide reply form only when a reply button is clicked

I am trying to add reply comments to my comment system in a Laravel 8 project. I have written a script to show the reply form when the reply button is clicked but the form is always in displayed by default. However, when the reply button is clicked, the form hides and shows on the second click. I seem not to get what I am not getting right. Please, I need your assistant in this regard. This is my code:

//The form    
<div class="card-body">
     @if ($PostDetails->comment)
         @foreach($PostDetails->comment as $comment)
             <blockquote class="blockquote">
             <p class="mb-0">{{ $comment->comment }}</p>
             <footer class="blockquote-footer">{{ $comment->user->name }}</footer>
             </blockquote>
             {{-- Reply Comment Section --}}
             <button id="reply-button" onclick="showReplyForm('{{$comment->id}}', '{{ $comment->user->name }}')" class="border-0 bg-transparent">Reply</button>
             <div class="row flex-row d-flex">
                  <form action="" method="post">
                       <div class="col-lg-12">
                            <textarea id="reply-form-{{$comment->id}}" cols="80" rows="2" class="form-control mb-10" name="replyMessage"></textarea>
                       </div>
                  </form>
              </div>
              <hr>
         @endforeach
       @endif
</div>

//The script is:
<script type="text/javascript">
    function showReplyForm(commentId){
        var x = document.getElementById(`reply-form-${commentId}`);
        if (x.style.display === "none") {
            x.style.display = "block";
        }else{
            x.style.display = "none";
        }
    }
</script>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Add the style="display:none;" to make it hidden at start.

<div class="card-body">
     @if ($PostDetails->comment)
         @foreach($PostDetails->comment as $comment)
             <blockquote class="blockquote">
             <p class="mb-0">{{ $comment->comment }}</p>
             <footer class="blockquote-footer">{{ $comment->user->name }}</footer>
             </blockquote>
             {{-- Reply Comment Section --}}
             <button id="reply-button" onclick="showReplyForm('{{$comment->id}}', '{{ $comment->user->name }}')" class="border-0 bg-transparent">Reply</button>
             
             <div class="row flex-row d-flex" style="display:none">
                  <form action="" method="post">
                       <div class="col-lg-12">
                            <textarea id="reply-form-{{$comment->id}}" cols="80" rows="2" class="form-control mb-10" name="replyMessage"></textarea>
                       </div>
                  </form>
              </div>
              <hr>
         @endforeach
       @endif
</div>

//The script is:
<script type="text/javascript">
    function showReplyForm(commentId){
        var x = document.getElementById(`reply-form-${commentId}`);

        if (x.style.display === "none") {
            x.style.display = "block";
        }else{
            x.style.display = "none";
        }
        
    }
</script>
about 4 years ago · Juan Pablo Isaza Report

0

Change the id attribute for your buttons to class="reply-button" and add the style="display:none" to your textareas. Then you can simplify your JavaScript part to

document.querySelectorAll(".reply-button").forEach(btn=>btn.onclick=ev=>{      
  const x=ev.target.nextElementSibling.querySelector("textarea");
  x.style.display = x.style.display ==="none"?"":"none";
});

Then there is no need to assign ids to the individual textareas anymore.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!