Utilizing RJS and Observer to Toggle Visibility of a DIV

In my RJS template, I have a process that includes:

1. Fading out a submitted form 2. Showing a response popup div 3. Bringing up a new form using a partial

My goal is to delay the display of the second form until the user clicks the "x" button on the popup.

The current RJS code looks like this:

unless @actor.health <= 0 || @actor.finished
  page.visual_effect(:fade, "act_form", :duration => 0.5)
  if @actor.acts.last.decision.consequence && @actor.acts.last.decision.consequence != ""
    page.insert_html :after, 'act_form_wrapper', :partial => 'consequence'
    page.assign 'popup', true
  end

  page.delay(1) do
    page.replace_html("act_form", :partial => 'act_form')
    if @prompt.decisions.count > 1
      page.assign 'decEmpty', true
    else
      page.assign 'decEmpty', false
    end
    if @actor.play.comment_req
      page << 'document.getElementById("act_comment").value = ""'
    end
    page.visual_effect(:appear, "act_form", :duration => 0.5)
  end

  page.replace_html("scores", :partial => 'scores')

else
  flash[:notice] = 'This game is finished. How did you do?'
  page.redirect_to(@actor)

Additionally, the popup div structure is defined as:

<span class="close_consequence"><%= link_to_function image_tag('close.png', :alt => 'close'), "$('.consequence_popup').remove(); popup=false;"  %></span>

I'm considering using the "popup" Javascript variable in conjunction with a while loop or observer to control the timing of displaying the second form based on its value. I'm not sure if this approach is feasible or if there's a better way to achieve this. The RJS functionality still remains somewhat mysterious to me.

Answer №1

To create an interactive user experience, I suggest hiding the new form using inline CSS and including a functionality in the popup dismiss action to show the hidden form.

<div id="new-form" style="display:none;">
  ...
</div>

<div id="popup">
  <div>Introducing: Popup!</div>
  <a href="#" onclick="$(this).hide(); $('new-form').show();">Click here to dismiss and view new form</a>
</div>

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

manipulating data using javascript

I have an array of objects like this: var arr = [{'name':'one','age':24},{'name':'two','age':21}] Then I assigned this array to another variable var newVar = arr. Next, I modified the value of a ...

Should WordPress files be kept separate (php, css, and js) or combined into a single file?

Currently, I am updating my WordPress website with a goal of minimizing the number of plugins used. To achieve this, I prefer writing code only for essential functionalities. In order to optimize my work with php, css, and javascript files, I have been exp ...

Turn off the scroll bar and mouse wheel functionality, while still allowing for scrolling

Is there a way to hide scrollbars on a website, prevent scrolling by mouse but still allow access to hidden areas using the jQuery scrollTo plugin function? (e.g. clicking on a button to scroll to a specific element) Thank you! ...

Error: The function $compile does not exist

Currently, I am working on developing an AngularJS directive using TypeScript. While testing my code in the browser, I encountered the following error: TypeError: $compile is not a function at compileComponent.js:14 Interestingly, the TypeScript compiler ...

Encountered an error when trying to access the DOM node in componentDidMount

Encountered a TypeError: Cannot read property 'offsetHeight' of undefined when attempting to retrieve the offsetHeight of a DOM node. componentDidMount() { setTimeout(function(){ console.log(this.contentBody.offsetHeight) ...

Is assigning an ID to multiple objects a poor decision?

While working on my Django project, the template quickly became cluttered with elements all having unique IDs. I could reduce the number of IDs by assigning them to their parent elements, but this would require longer jQuery code. So now I'm faced wit ...

How to effectively use the iOS virtual keyboard's navigation buttons: "previous", "next", and "Go/Enter

Working on a mobile app using jquery mobile, php, phonegap, and cordova. I need to add next and previous buttons to the virtual keyboard for all form elements like text boxes, and show a Done/Go/Enter option for the last text box. How can I programmaticall ...

JQuery .click function functioning properly on alternate clicks

Currently, I am integrating JQuery with ReactJS. However, there seems to be an issue where the action that should occur when clicking a button only works on every other click. The first click doesn't trigger anything, but the second one does. I attem ...

Having trouble accessing data from the local storage?

const headers = new Headers({ 'access_token' : accToken, 'Content-Type': 'application/json', }); axios.post(baseURI, data, { headers: headers }) ...

Attempting to divide the given web address

Is there a way to divide the URL below into components and extract only "store" from it? http://www.store.com/products.aspx/Books/The-happy-donkey If so, what would be the best method to achieve this? ...

What is the best way to divide a string and then insert a hyperlink into the first part of the string?

Recently delving into the world of JavaScript, I find myself tackling a perplexing challenge involving an RSS Feed. Upon extracting an item from said feed, the output is as follows: Google Home Page http://www.google.com My current puzzle is how to se ...

Vue.js and Firestore will only update variables that do not have empty string values

Modify the variables that are not empty strings const state = reactive({ birthNumber: '', phoneNumber: '', DoctorName: '', DoctorPhone: '', }) db.collection(state.user.uid).doc( ...

Shiny Exterior using ThreeJS

Trying to achieve a realistic metallic appearance in ThreeJS has been a challenge for me. Despite knowing that I should be using the MeshPhongMaterial material type, I am struggling to configure it properly. My current implementation only results in a pla ...

Iterating through an array of objects within a Vuejs template

Consider the JSON data below: { "games": [ { "id": 1, "init_date": "2020-02-11T07:47:33.627+0000", "players_in_game": [ { "id": 1, "player": { "id": 1, "player": "Jack Bauer", ...

Adjust the size of the font in accordance with the value of the span text

I am looking to adjust the font size based on the value within the span element. The numbers in the class name may vary. Issue: using text() retrieves all values (e.g. 50020) I want to incorporate each() and $this in some way. How can I do this? Thank ...

Endless loading on NodeJS server local host

My NodeJS setup is serving files, but the page seems to be stuck loading. Here's a snippet from my index.js file: const express = require("express"); const path = require("path"); const http = require("http"); const socke ...

Web Security Vulnerability: Cross Site Scripting Detected

In our code, we are aiming to prevent XSS (Cross Site Scripting) attacks. However, the solution may involve a combination of JS (JavaScript) and HTML escaping, which could prove to be quite challenging. Below is a snippet that closely resembles our code: ...

Is there another reason why Synchronous XMLHttpRequest on the main thread is no longer supported?

After doing extensive research on this particular issue, I came across a myriad of questions and answers related to it. One post on Stack Overflow stood out as it had various answers with different causes to the same problem. However, none of them seemed t ...

Divide a list Observable into two parts

In my code, I have an Observable called 'allItems$' which fetches an array of Items. The Items[] array looks something like this: [false, false, true, false] My goal is to split the 'allItems$' Observable into two separate Observables ...

Is it possible to utilize CSS to alter the header content when the window is minimized?

I am currently experimenting with replicating a website design from a popular clothing brand to enhance my knowledge of HTML and CSS. I am curious to know if it is possible to modify the header content when the window size is reduced, either through CSS al ...