Press on the div to reveal its content at the clicked position

Hello, I have a query that I need help with.

I currently have a basic table with buttons in columns that act as filters. When you click on one of these buttons, it opens up a form.

https://i.sstatic.net/nuEpq.png

What I am trying to achieve is to make the form appear at the exact coordinates of the button when it is clicked. I have attempted to retrieve the coordinates of the button and modify the form accordingly, but I have encountered some difficulties.

As a testing measure, I tried the following:

document.getElementById('myForm').style.top = "100px;";

However, this method did not work as expected.

Here is the structure of the form:

<div class="form-popup" id="myForm">
    <div class="form-container">
        <h1 id="filtername">Filter</h1>
        <div class="row">
            <div class="col-6">
                <label>Set minimum</label><input id="mininfilter" class="form-control" type="number" value="0">
            </div>
            <div class="col-6">
                <label>Set maximum</label><input id="maxfilter" class="form-control" type="number" value="0">
            </div>
        </div>
        <br>
        <button type="button" id="filtersave" class="btn">Apply</button>
        <button type="button" class="btn cancel" onclick="closeForm()">Close</button>
        <button type="button" id="back_to_normal" class="btn warning"><i class="material-icons">
            autorenew
            </i></button>
    </div>
</div>

And this is the corresponding CSS:

.form-popup {
display: none;
position: fixed;
bottom: 0;
right: 0;
border: 3px solid #f1f1f1;
z-index: 9;

I appreciate any assistance you can provide. Thank you!

Answer №1

One of the steps I took was to select the div element:

var formDiv = document.getElementById("myForm").style;

I then proceeded to capture the Y and X coordinates when the button is pressed:

var xCoordinate = event.clientX;
var yCoordinate = event.clientY;

After obtaining these variables, I adjusted the form's position based on the coordinates:

formDiv.top = yCoordinate + "px";
formDiv.left = xCoordinate + "px";

Answer №2

Visit this link for a Bootstrap modal example.

We recommend using a Bootstrap modal for this. You can find the steps and customization options in the provided link. Don't forget to filter your results!

        <br>

    </div>
</div>

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <label>Set minimum</label><input id="mininfilter" class="form-control" type="number" value="0">


            <label>Set maximum</label><input id="maxfilter" class="form-control" type="number" value="0">

        <button type="button" id="filtersave" class="btn">Apply</button>
        <button type="button" class="btn cancel" onclick="closeForm()">Close</button>
        <button type="button" id="back_to_normal" class="btn warning"><i class="material-icons">
                autorenew
            </i></button>
    </div>
</div>
</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

Tips for transforming a date into a time ago representation

Can someone help me with converting a date field into a "timeago" format using jquery.timeago.js? $("time.timeago").timeago(); var userSpan = document.createElement("span"); userSpan.setAttribute("class", "text-muted"); userSpan.appendChild(document.crea ...

Issue: Unhandled TypeError: Problem detected when calling storage.set(object items, optional function callback) in a Chrome Extension

Attempting to create my first Chrome extension, focusing on blocking access to specific websites like Facebook or Instagram. Using code to close tabs if the blocked sites are accessed. In a separate HTML file, providing users with two radio button options ...

JavaScript Regular Expression that identifies commas enclosed within quotation marks

I am attempting to parse a text-based log file in the following format: type: 'click', category: 'REFRESH_DOOR', desc: 'clicked refresh from door 0124', site: 'mysite', pathname: '/load_areas/all/doors&apos ...

Press on the row using jQuery

Instead of using link-button in grid-view to display a popup, I modified the code so that when a user clicks on a row, the popup appears. However, after making this change, nothing happens when I click on a row. Any solutions? $(function () { $('[ ...

What is the best way to extract JSON data from a remote URL?

Having recently started with JavaScript, I am looking to parse JSON from an external URL using pure JavaScript. Currently, I have the following code: var getJSON = function(url, callback) { var xhr = new XMLHttpRequest(); xhr.open('GET', url, tr ...

Step-by-step guide to positioning an iframe with 'transform:scale' on the left side

Recently, I encountered an issue with an iframe in my project. Initially, without any 'transform:scale' css applied, it displayed on the top-left side of the div as expected. However, after adding -webkit-transform:scale(0.6,1); margin-left:0.0e ...

What is the purpose of pressing the button a second time once the Conditional Statements are no longer

$(function() { $("button").click(function() { if ($(this).text() === "3") { console.log("correct"); $(this).text("correct") } else { console.log("wrong"); $(this).text("wrong") } }) }) <script type="text/javascri ...

Guide to Utilizing the Import Function in a Vue 3 Template

Working on a Vue 3 project, my setup includes a stuff.ts file with helpful functions that I need to utilize in my template. <script lang="ts"> import { defineComponent, onMounted } from 'vue' import { doSomething } from ' ...

Achieve maximum height with background images

Having trouble with responsiveness on my box containing a background image. I am aiming for the box to adjust its height based on the background image, as I have set the background-size attribute to auto. html: <div class="index_boxs" id="discover_win ...

CSS code preventing hyperlink from functioning

My website is almost complete, but I'm running into an issue with some hyperlinks on my event registration page. The first link works fine, but the second one does not. To troubleshoot, I created a test page with just the two links and still encounter ...

Is it necessary for the keys in separate loops to be unique among siblings?

Does the use of key in loops create separate branches, or do they still need to be unique for the same parent? Take a look at this example: // This is obviously an error: <div> <div key="gimme-a-break" /> <div key="gim ...

Implementing a dual hover effect on a Div element

I am working with HTML code that includes an image and text <div class="subcontainer1"> <img src="a.png" alt="" class="imgcolumn"> <h3 class="header3">Hello</h3> </div> This setup places the content above the image. ...

What is the best way to automatically collapse the Bootstrap navbar on smaller devices after selecting a menu item?

Here is the code I've implemented for executing the task. It functions correctly when resizing the window on a computer but does not work on mobile devices. $('.nav li a').on('click',function(){ $('.navbar-collapse') ...

Unit testing in Typescript often involves the practice of mocking

One challenge with mocking in Typescript arises when dealing with complex objects, as is the case with any strongly-typed language. Sometimes additional elements need to be mocked just to ensure code compilation, such as using AutoFixture in C#. In contras ...

Experiencing a "non-serializable value found in the state" error specifically while utilizing redux toolkit, but this issue does not occur with traditional redux implementations

While transitioning my app to utilize Redux Toolkit, I encountered an error after switching over from createStore to configureStore: A non-serializable value was found in the state at path: `varietals.red.0`. Value:, Varietal { "color": "red", "id": " ...

What causes the other array to be affected when one is changed?

Take a look at this snippet of JavaScript code: var x = [1, 2, 3], y = x; y[1] = 3; x; // x === [1, 3, 3] Why does this happen? Why is the value of "x" changing when I update "y[1]"? I tried it in both Firefox and Chrome and got the same result. Th ...

Building a TypeScript Rest API with efficient routing, controllers, and classes for seamless management

I have been working on transitioning a Node project to TypeScript using Express and CoreModel. In my original setup, the structure looked like this: to manage users accountRouter <- accountController <- User (Class) <- CoreModel (parent Class o ...

Preventing parent requests from being triggered when a child element is clicked in Angular 2

I have a similar code structure as shown below and I am trying to achieve the behavior where clicking on the Child Div does not trigger the Parent Div click event. <div class="parentDiv" (click)="parentDiv()"> <div class="childDiv" (click)="ch ...

Ways to ensure a video completely fills the frame

I am experiencing an issue where the video in the main div of my home page does not fully fill the div right away - I have to refresh the page for this to happen. As a result, there is black space on the left and right sides. Interestingly enough, this pr ...

Utilizing the ref received from the Composition API within the Options API

My current approach involves utilizing a setup() method to bring in an external component that exclusively supports the Options API. Once I have imported this component, I need to set it up using the Options API data. The challenge I face is accessing the ...