Position a div adjacent to a particular, changing Rails component once jQuery has toggled it

Within my Rails application (which is utilizing Bootstrap's grid system), I am featuring a blog post within the left column. On the right side, I have integrated a jQuery toggle effect to reveal a quick form for user interaction such as leaving comments.

To activate this form, I have configured my jQuery script to toggle it when a paragraph tag is clicked, successfully functioning in doing so. However, the issue arises where the form with the id of #exampleToggle ends up posting at the very top of the column after toggling.

Query: How can I ensure that the form/exampleToggle not only toggles in but appears next to the specific paragraph tag that triggers its display?

<div class="col-md-8">
    <% @posts.each do |post| %>
            <h2 id="title"><%= post.title %></h2>
            <p id="category"><%= post.category %></p>
            <div id="text"><%= markdown(post.content) %></div>    
    <% end %>
</div> 

<div class="col-md-4">
<div class="exampleToggle">
    <%= form_for :cements do |f| %>
        <div class="form-group">
        <div class="field">
        <%= f.label :username %><br>
        <%= f.text_field :username, class: "form-control" %>
            </div>
            </div>

        <div class="form-group">
        <div class="field">
        <%= f.label :post %><br>
        <%= f.text_area :post, class: "form-control" %>
      </div>
      </div>

<div class="actions">
    <%= f.submit "Save", class: "btn btn-success-outline" %>
</div>
<% end %>
</div>

<script>

    $(document).ready(function(){
        $( ".exampleToggle" ).hide();

        $("p").click(function(){
            $(".exampleToggle").toggle();
            });
    });

</script>

If the page were static, resolving this would be relatively straightforward. For instance, having three paragraph tags arranged within corresponding rows using the grid system and triggering the form to appear within its respective row via the correct id selector.

Given that the content is dynamic due to potential paragraphs generated by the Rails loop, the earlier solution fell short.

Furthermore, I am open to abandoning the use of the Bootstrap grid if a more effective approach exists. Currently, I have allocated space on the right column for placing the form - the challenge lies in positioning the exampleToggle adjacent to the relevant paragraph tag upon activation.

Is achieving this level of functionality possible?

Answer №1

It is important to consider how you will populate the pop out boxes before implementing this code snippet. Below is an example of using jQuery to append content and positioning it based on the clicked element using CSS.

$(document).ready(function(){
  $("p").click(function(){
    $(this).append("<div class='pop-out'>populate this dynamically</div>");
  });
});

Next, include the following CSS:

p {
  position: relative;
}

.pop-out {
  position: absolute;
  top: 0;
  left: 100%;
  width: 100px;
  height: 100px;
}

If you provide a sample JSFiddle, I can offer assistance with troubleshooting any issues that may arise.

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

Align the div to the left with text or a paragraph displayed on the right side

Check out my js fiddle below: https://jsfiddle.net/9yj63s3f/ Here is the CSS code snippet: .viddiv{ float:left; width:50%; } .vidholder{ position:relative;height:0;padding-bottom:50%; width:50%; float:left; } iframe{ position:abso ...

Implementing color transitions in javascript

Everyone talks about transitioning things in CSS, but have you ever thought about transitioning background colors in JavaScript? I'm currently working on a function that changes the background color of a div with the id of ex1 to green. However, I&apo ...

Can a FontAwesome icon be used as a button in Bootstrap?

Hi there, I'm trying to use a fontawesome icon as a button in my code. Here is what I have so far: <!DOCTYPE html> <html lang="en"> <head> <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" ...

Tips for preserving order of items in MVC framework

I have been working on organizing my carousel items and I need a sortable list view to help me choose the right format for the site. I managed to make the items sortable using this jQuery command: $(function() { $("#subsortsortable tbody.content") ...

Exploring Selenium: Clicking on Auto-Complete Suggestions using Python

Attempting to interact with an auto-complete search bar on the site in order to search for results. Wanting to click on the drop-down element that appears after entering a city name to perform a full city name search and obtain results. Below is the cod ...

Using float instead of CSS inline-block is more effective for styling a PHP element

I've been working on styling a page that was generated with App Gini. While I have been successful in editing most elements so far, I am facing an issue with getting inline-block to work properly. Although float:left is functioning correctly, I would ...

What is the best way to ensure my images are responsive to the varying device sizes when displayed on this website?

In my endeavor to design a 2-column layout using Bootstrap, I aim for each row to consist of two columns: One column will contain text, while the other will display images that complement the textual content. Below is the Bootstrap code snippet for the n ...

Step-by-step guide to populating a JavaScript array with AJAX requests

Having some trouble with populating a JavaScript array using an Ajax request. Here is the code I'm working with. The Ajax portion seems to be running smoothly. function GetColumns() { var customArray = []; $.ajax({ url: '@Url.Con ...

Choose a textarea in a generic manner

Can anyone help me with selecting a textarea from a specific element on my webpage using jQuery? I've been trying different methods without success. Here is an example of what I have attempted: JsFiddle I wanted to use the :input[type='blah&ap ...

Creating an Eye-catching Presentation: Tips for Adding Text to Your CSS3 Slideshow

I am currently in the process of creating a CSS3 slideshow with text for each image. I found inspiration from this example: . However, I made some modifications to make it responsive. Here is the HTML code I have used: <div id="slideshow"> < ...

Tips for Aligning Images of Various Sizes in the Center of a div

My images are displayed in a horizontal line, but some have different sizes. I want to center all the images within their container div for a better look. Is there a way to do this automatically with CSS so that the images remain centered regardless of siz ...

Lack of animation on the button

Having trouble with this issue for 48 hours straight. Every time I attempt to click a button in the top bar, there is no animation. The intended behavior is for the width to increase and the left border color to change to green, but that's not what&ap ...

Checkbox inputs with activated labels causing double events to fire

Creating round checkboxes with tick marks dynamically and appending them to id="demo" on the click of two buttons that invoke the get(data) method is my current goal. The issue arises when both buttons are clicked simultaneously, as the checkboxes do not ...

Moving from CSS to SCSS: Incorporating CSS files from npm packages - A Step-by-Step Guide

I am new to SASS and currently in the process of converting my existing project's CSS files to SCSS. I know that simply changing the extension to .scss will work for my custom files, but I'm not sure how to handle the dependency style files. I ty ...

Create a payment invoice using the power of AJAX, jQuery, PHP, and MySQL

Having issues with my bill calculation. When adding multiple products, the price of all products appears to be the same. I'm unable to calculate the total correctly and struggling to fix this. Please review my code and provide insights. bill.php < ...

Issues persist with Ajax form submissions; the submitted data never seems to go through

I have encountered variations of this issue multiple times, but despite analyzing numerous examples, I am unable to determine why my code is not functioning properly. <script> $('document').ready(function(){ $('datafixForm' ...

What benefits does utilizing unordered lists provide in navigation bars?

Throughout my research, I have come across numerous tutorials detailing the process of creating a navigation bar. They often begin by adding an unordered list and then proceed to remove the default styling such as bullet points and margins from both the ...

What is the best way to display JSON data in a Listview control?

What is the best way to display JSON data in a list format? Currently, I am able to retrieve JSON data and display it in an alert dialog. The JSON data looks like this: [{"_id":"5449f20d88da65bb79a006e1","name":"name3","phone":"888888","service":"service ...

The minmax() function in a responsive grid layout does not function properly when incorporating grid areas

My Vision for the Layout I am striving to create a dynamic grid structure with three columns and two rows, where the third column spans both rows and features an image. This layout should adjust itself based on the device screen size: Specifically, I wou ...

Adjust the size of a div using absolute positioning

Can a div with absolute position be resized? I need this pink modal to maintain the same width as the input and resize accordingly. This modal will be utilized in a dropdown component, so it should resize along with the input. Moreover, is using absolute ...