Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7832965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:30:38+00:00 2026-06-02T12:30:38+00:00

I have a show and hide div that shows and hides comments when clicked.

  • 0

I have a show and hide div that shows and hides comments when clicked. By default before it’s clicked it shows a message count plus some down arrows e.g.

“55 Comments [down arrows]”

and when clicked it changes to:

“Hide comments [up arrows]”

I use this code for displaying the amount of comments by default and the arrow (users/new):

    <span class="commentsCount"><%= pluralize(m.comments.count, 'comment') %></span>
    <span class="commentArrows"><%= image_tag 'view_all_comments.png' %> </span>

I use JQuery to help achieve the changes (users/load_comments.js.erb):

$('.postHolder').off().on('ajax:success', '.view_all_comments', function(){

    var postContent = $(this).parents('.post_content'),
        commentContainer = postContent.find('.comment_container'),
        getComments = "<%= j render 'users/partials/show_all_comments' %>";
        commentContainer.slice(0, -2).remove();

// The next 2 lines replace the comment count with "Hide comments" and up arrows"

          postContent.find('.commentsCount').html("Hide comments");
          postContent.find(".commentArrows img").prop("src", "/assets/up_arrows.png");

             postContent.find('.comment_container:first').before(getComments);
});

Now the important part is when Hide comments is clicked again. I need it to revert back to showing comments count. I can revert the arrows back to original arrows but the text I don’t see a way of reverting back to the comments account because I can’t access rails helpers and my instance variables etc inside my assets/javascripts/users.js file.

Here is the code (assets/javascripts/users.js:

    // for showing and hiding comments

        $('.expandComments').click(function() {

        var showAllCommentsSubmit = $(this).find('.showAllCommentsSubmit'),
            postContent = $(this).parents('.post_content'),
            commentContainer = postContent.find('.comment_container');


            if ($(this).hasClass('visible')) {
                postContent.find(".comment_container").slice(0, -1).hide();
                postContent.find(".commentArrows img").prop("src", "/assets/view_all_comments.png");

//Here is where I need the code to replace the "Hide comments" text.

            }  else {


                if (showAllCommentsSubmit.is(':disabled')) {
                    postContent.find(".comment_container").show();
                    postContent.find(".commentArrows img").prop("src", "/assets/hide_comments.png");

                } else {
                    showAllCommentsSubmit.trigger('submit');
                    showAllCommentsSubmit.prop({disabled: true});
                    postContent.find(".commentArrows img").prop("src", "/assets/comments-loader.gif");
                }
            }

            $(this).toggleClass('visible');

        });

Since I don’t think it’s possible to use embedded ruby / rails helpers in the assets javascript files how would I be able to achieve what I’m trying to achieve.

Is there some way to display some html on top of other html then remove it when it’s not needed any more and have the html underneath revealed again?

I’m sure there are some other solutions I can come up with such as bring some css into play.. having a div hide and show but I haven’t figured how I’d do that exactly. Wondering if there is a quick solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T12:30:39+00:00Added an answer on June 2, 2026 at 12:30 pm

    You can use Rails helpers in asset code. You essentially just need to rename your asset from users.js to users.js.erb, and Rails will preprocess it with ERb before emitting the content as JavaScript.

    Check out the Rails guide on preprocessing in the asset pipeline for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some JQuery code that shows or hides a div. $(div#extraControls).show(); // OR
I have 3 radio buttons which hide/show some div's. The radio buttons are getting
I have created some functional categories that I use to show/hide markup elements. However,
I have a div called MyDiv and I have some code that hides and
I have a select menu that shows/hides the livetransopts div when an option is
I have a div show hide function which is working fine. when I click
I have this jquery script which suppose to hide/show div element base on the
I have done a hide and show of a div element, on clicking a
I have a simple function that shows and hides elements if a user clicks
I have a function that displays comments in DIV box from a database. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.