Concealing an Automatically Updated Section when Devoid of Content -- Ruby on Rails Version 4

I have come across various solutions to this problem, but none of them seem to be effective for my specific project. In my application, the user's previous choices are displayed in multiple divs. Initially, all the divs are empty. As the user progresses through the questions, each answer is shown in a separate div until they reach the final result page.

However, I am struggling to find a way to hide these divs until they are populated with content.

All my attempts so far have revolved around using jQuery's $('.div:empty').hide, but without success. Can anyone point out what I might be doing wrong?

Views/Layouts/_user_choices.html.erb

  <div class = "default_test" id = "first_bar" >
    <% if @category != nil %>
            <p><%= @category.title %></p>
        <% else %>
            <!-- the div remains empty -->
    <% end %>
 </div>

assets/javascripts/application.js:

$(document).ready(function() {
    $('.default_test:empty').hide();  // unfortunately, this does not work as intended
});

Answer №1

When examining the content of this page regarding :empty, it is evident that neither of these examples will match :empty

A DIV element with a space inside

<div> </div> 

A DIV element that closes on a different line:

<div>
  <!-- test -->
</div>

<div>
</div>

To use :empty, your div should look like this:

<div></div>

Alternatively,

<div><!-- test --></div>

You may also want to try eliminating line breaks and extra spaces:

<div class = "default_test" id = "first_bar" ><% if @category != nil %><p><%= @category.title %></p><% else %><!-- the div remains empty --><% end %></div>

Answer №2

Discovered the solution - and it appears quite absurd. Simply do this:

<div class = "default_test" id = "first_bar" >
    <% if @category != nil %>
           <p><%= @category.title %></p>
    <% else %>
           <!-- the div remains empty -->
    <% end %>
</div>

...and remove all the spaces to put everything on a single line.

<div class = "default_test" id = "first_bar"><% if @category != nil %><p><%= @category.title %></p><% else %><!-- the div remains empty --><% end %></div>

Strangely enough, it functions in this arrangement but not in the initial version with spaces. It's truly puzzling that it only works in this specific way. If there is a more efficient approach, I'm open to suggestions. Seems odd that it should only work when formatted like this.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Create HTML div elements dynamically with JavaScript based on database information

Can javascript be used to create divs from database information? ...

Encountering an issue with re-rendering components in REACT when using the setState hook

Is there a way to efficiently change the inline style property of multiple items based on their position info stored in a useState hook? I want to update the CSS value of all items except for the one that was clicked on. I tried using setState to achieve t ...

Generating dynamic dropdown menus using data from a database with the help of PHP and Ajax technologies

I'm currently working on creating a dynamic dropdown menu that will be populated with data retrieved from a database. I've hit a roadblock in parsing the data from a multidimensional array sent by a PHP file. Here's a snippet of my code: Se ...

Trilateration of three-dimensional points using K vertices on each face

I'm currently working on a project using Three.js and I have a set of 3D points in the form of (x, y, z) coordinates, as well as a series of faces. Each face consists of K points and can be either convex or concave in shape. Despite consulting the doc ...

I am continuously encountering an error message saying [$injector:modulerr] while working with AngularJS

I've been reviewing my JavaScript code thoroughly, but I can't seem to pinpoint any major issues. The error message I'm encountering is as follows: Uncaught Error: [$injector:modulerr] Failed to instantiate module careApp due to: Error: [$i ...

What is the best way to send form values to the server using Ajax and Jquery?

Is there a smart way to utilize Ajax and jQuery for sending all input values from a dynamically generated form to the server? Manually listing each input name in the Ajax post seems cumbersome, so I'm wondering if there's a more elegant solution? ...

Trouble arises when attempting to execute a Vue method through a Vue computed property

It seems that the issue at hand is more related to general JavaScript rather than being specific to VueJS. I have a Vue Method set up to make a Firebase Call and return the requested object, which is functioning properly: methods: { getSponsor (key) { ...

Is it feasible to display a message for a certain duration without using the alert() function upon clicking a button?

I'm working on a NEXT.JS project and I need to display a <p> element under my button for a specific duration before it disappears. I don't want to use the alert() function. Any suggestions on how to achieve this? ...

Using values from a designated line within the textarea to successfully submit a GET form

How can I extract values from a specific line in a TextArea and use them to submit a form? <!DOCTYPE html> <html> <body> <input type='button' value='submit' onclick='document.getElementById("submit-line-3") . ...

Ways to stop a JavaScript function from running during page loading

After clicking the submit button on my form, I want to send a confirmation post using the sendPostAjax() function before submitting the form to the payment provider. However, I'm facing an issue where the sendPostAjax() function is getting executed as ...

Tips for displaying the data on top of individual column bars: Hightcharts, Vue-chartkick, and Cube Js

Looking for assistance with adding value labels to the top of each column bar in a Vue chart using chartkick and highcharts. https://i.sstatic.net/c4Bwc.jpg Check out the image above to see my current output. <template> <column-chart lable= ...

Transformed 700 audio players compartmentalized within a nested tab interface. Optimal tab coding techniques include jquery, ajax

We are currently working on developing a nested tab interface that will include 700 audio players for MP3 files all on the same page within various tabs. However, only one audio player will be visible at a time and will only appear when the tab is clicked. ...

Utilizing grease/tampermonkey (or any other browser extension) to filter out specific characters within webpage elements

Forgive me if my language is not accurate, as I am still learning about programming. The forum that I visit has cluttered the page with unnecessary and glitchy images and text for a promotion. This has made the forum difficult to navigate. I was successful ...

challenge with altering data in transmission when using the GET method

$('.textedit').editable('/webpage/skeleton/edit_text/text/process', { loadurl: '/webpage/skeleton/edit_text/loadraw', loaddata: {id: $(this).attr('id'), client: $(this).data('client')}, submitda ...

The versions of my npm and node are not compatible, despite using nvm

I have recently started working with node and npm. I need to run a program repository for my job, which requires compatibility with node version 10.13.0 or even 8.11. I attempted to install nvm, but now every time I try to execute any npm command (includ ...

What could be causing the queuing of multiple Ajax requests in ExtJS?

I am encountering an issue with my grid setup. I have a menu on the left side for each item on the grid, and this menu's items change based on the selection in the grid. When the event selection is triggered, an Ajax.request function is called to hand ...

Vue 3 allows for creating multiple cards with unique contents

I received this outcome: Content duplicated but not cloned as a card element Below is the code snippet <script setup> import { ref } from 'vue'; defineProps({ project: String, }); const projectList = ref([ { img: './src/asse ...

Embed your JavaScript code within the header tags of the WooCommerce order confirmation page

Seeking to enhance my thank you page with Google tracking script, I have developed code that successfully injects the tracker within the of the page, incorporating dynamic values. However, my goal is to embed it within the tags instead. function mv_goog ...

Inform the user that an error has occurred when attempting to perform an invalid

While using redux promise middleware for my front end, I am wondering about the correct status code to throw from my backend in case of an error. I know that I can use res.status(500).json(something), but is 500 the appropriate code for all types of erro ...

jquery plugin causing issues with bootstrap button functionality

Currently, I am utilizing the jQuery form plug-in to post my form in an Ajax way. The post function is functioning perfectly with the default button. However, it seems to encounter issues when I try to use a custom Bootstrap button as shown below. Could so ...