Employing jQuery's offset() method for scripting

Issue: The problem I'm facing is that after clicking on the label where the date should be entered, the calendar is not appearing as expected. I tried using a jQuery script to darken the background, but then the calendar is not displaying in the correct position. I attempted to use the offset() function, but it doesn't seem to be working:

(part of) Code :

< script > // code with issue
  $(document).ready(function() {
    //Get
    var p = $(".e_darker");
    var offset = p.offset();
    //set    
    $(".calendar_in_place").offset({
      top: offset.top,
      left: offset.left
    });
  }); <
/script>

<
script > // scrolls down page to input label
  $(function() {

    $("#radio_eventi").click(function() {
      $('html,body').animate({
          scrollTop: $("#radio_eventi").offset().top
        },
        'slow');
    });
  }); <
/script> <
script > // makes dark background appear/dissapear
  $(document).ready(function() {
    $(".e_darker").click(function() {
      if ($("#darker").show(150)) {
        $(document).mouseup(function() {
          $("#darker").hide(150);
        });
        $(".e_darker").mouseup(function() {
          $("#darker").hide(150);
        });
      } else {
        $("#darker").show(150);
      }
    });
  }); <
/script>
.calendar_in_place {
  top: 0 !important;
  left: 0 !important;
}

.in-front {
  z-index: 6;
}

#darker {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  background-color: black;
  opacity: 0.5;
  display: none;
  transition: opacity 0.15s linear;
  z-index: 5;
}
<head></head>

<body>
  <div id="darker"> </div>
  <!-- div that makes background darker -->
  <!-- imput label -->
  <section id="eventi_spec" class="nascondi">
    <div class="form-group">
      <label for="datetimepicker11" class="label_no_middle">Data di avvio dell'evento</label>
      <input type="text" placeholder="GG/MM/AAAA" data-mask="99/99/9999" class="form-control in_front e_darker" id="datetimepicker11">
    </div>
</body>

Site uses Bootstrap.

Answer №1

Upon reviewing your code, it appears that the issue lies with the styles applied to .calendar_in_place. The use of !important is causing it to override the values set by jquery offset.

To resolve this, consider using CSS() instead and removing the !important declaration.

Refer to the snippet below for the .test div:

    //Get
    var p = $(".e_darker");
    var offset = p.offset();
    //Set    
    $(".test").css({ top: offset.top, left: offset.left })
.calendar_in_place {
  top: 0 !important;
  left: 0 !important;
}

.test {
  top: 0 ;
  left: 0 ;
  width: 100px;
  height: 100px;
  background: red;
  position: absolute;
}

#darker {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 100%;
  background-color: black;
  opacity: 0.5;
  display: none;
  transition: opacity 0.15s linear;
  z-index: 5;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="darker"> </div>
<!-- div that makes background darker -->
<!-- input label -->
<section id="eventi_spec" class="nascondi">
  <div class="form-group">
    <label for="datetimepicker11" class="label_no_middle">Data di avvio dell'evento</label>
    <input type="text" placeholder="GG/MM/AAAA" data-mask="99/99/9999" class="form-control in_front e_darker" id="datetimepicker11">
  </div>
  <div class="test">
  </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

Using the spread operator in ES6 allows for arguments to be placed in a non-con

When working in nodeJS, my code looks like this: path = 'public/MIN-1234'; path = path.split('/'); return path.join( process.cwd(), ...path); I was expecting to get: c:\CODE\public/MIN-1234 but instead, I got: `‌publ ...

Stop the selection of text within rt tags (furigana)

I love incorporating ruby annotation to include furigana above Japanese characters: <ruby><rb>漢</rb><rt>かん</rt></ruby><ruby><rb>字</rb><rt>じ</rt></ruby> However, when attemp ...

I am attempting to save the input value to an array in local storage using VUEX. However, my current method is not producing the desired results

I am facing an issue where I am trying to push an input value and store it in an array in the local storage using VUEX. I have created an array in the state for this purpose. Initially, I push the value and then I stringify it to set the value to the sto ...

How can I trigger an event again after loading dynamic HTML content?

There is a clickable div#1 that expands another div#2. During the initial load, both div#1 and div#2 display dynamic content. However, when the dynamic data is refreshed, div#1 and div#2 are rewritten causing the expand event to no longer trigger. Any th ...

Transferring information through Ajax to PHP

When using AJAX to send a string via the GET method, I've noticed that punctuation characters sometimes don't appear when echoed in PHP. For instance, sending "sub + 12" results in PHP echoing "sub 12", and sending "&()*" echoes an empty str ...

When a link is clicked, update the href with a new destination and then trigger another click on the same link using jQuery

I am facing a situation where I need to change the href value of a link upon clicking it using Jquery. For example, let's say I have a link with the href /students/6691/movestudenttonewgroup When I click on this link, I want to prevent the default e ...

The stream.write function cannot be executed as a callable expression

Struggling to create a function that accepts either a writable stream (createWriteStream) or process.stdout/.stderr in TypeScript, but encountering an error. import { createWriteStream, WriteStream } from 'fs' const writehello = (stream: NodeJS. ...

Determine the Total of Various Input Numbers Using JQuery

There are 3 input fields where users can enter numbers, and I want to calculate the sum of these numbers every time a user updates one of them. HTML <input class="my-input" type="number" name="" value="0" min="0"> <input class="my-input" type="n ...

Incorporating accordion functionality using jQuery within my Model-View-Controller

Struggling to integrate accordion jquery with my MVC view. I want it to show content when @item["name"] is selected, but currently it's not working. Here is the script I'm using: <script> $(function () { $("#parameter_accordion").acco ...

The sizing of elements in Firefox seems to be off when using percentage

I am facing an issue with a page that has 3 td elements all set at a width of 33.333333%. In Firefox, the browser seems to be computing it as an actual pixel value of 568px, causing the containers to run off the page. I have not defined any fixed widths in ...

Displaying information in form using ajax within the laravel framework

I am currently utilizing ajax to retrieve data from a database. While I am able to successfully retrieve the data on the backend, I am facing difficulties displaying it in the input field below. I have tried writing some code, but it seems that the JavaScr ...

Using conditional rendering to compare the outcome of an asynchronous function within v-if condition

I have a Vue component for a sidebar with the following template: Vue.component('navigation',{ template:` <ul class="nav"> <li v-if="checkIfUserIsAdmin() == true" id="adminMenu"></li> <li id="userMenu">& ...

The function Server.listeners is not recognized by the system

Currently, I am following a tutorial on websockets to understand how to incorporate Socket.IO into my Angular project. Despite meticulously adhering to the instructions provided, I encountered an error when attempting to run my websockets server project: ...

various designs for multi-location infrastructure in angular js

Our angular app is designed with a multi-site architecture, where the base URL of each domain varies. Each site requires a unique header and footer to be incorporated, along with custom CSS and links specific to that site. Is there a way to dynamically in ...

Displaying a hidden div using the jQuery .each() method

Attempting to validate a form using jQuery, the goal is to target all form fields with class="required" and utilize the .each() function to verify if the field is empty. If it is empty, a hidden div positioned relative to the field should be displayed. An ...

Guide to updating information inside of script tags in html using javascript

Within my HTML, there is a script tag that looks like this: <script type="application/ld+json"> { "@context": "http://schema.org", "@type": "VideoObject", "name": "Title", "description": "Video descrip ...

Initiate a click action upon receiving a focus event

I can't seem to figure out why this code is not functioning as expected: $('a').focus( function() { $(this).click(); }); Context: I am working on developing a form where navigating through different elements (such as textboxes) will au ...

svg rect mistakenly appearing as a rounded polygon

I found a cool SVG pie chart on this codepen that I want to modify. The original chart is 50 x 50 and animated, but I want to make it 20 x 20. To resize the pie chart, I tried changing the width, height, radius, and center points in the code. I also adjus ...

Concentrate on Selecting Multiple Cells in ag-Grid (Similar to Excel's Click and Drag Feature)

Is there a way to click and drag the mouse to adjust the size of the focus box around specific cells in ag-Grid? This feature is very handy in Excel and I was wondering if it is available in ag-Grid or if there is a workaround. Any insights would be apprec ...

Ensure jQuery functions are reset following an AJAX request

Encountering an Issue with the .load Method: On my website, I am using the following code to toggle a button: $('.button').click(function(){ // function for button }); I attempted to use an AJAX call to load a div without refreshing the page. ...