Exiting the modal/popup box is currently disabled

I am struggling to figure out why the modal box/pop up is not closing when I click 'x'. It seems completely unresponsive.

Check out the JavaScript code below:

var kite = document.getElementById("poptext");
var kitetwo = document.getElementById("poptexttwo");
var closebtn = document.getElementById("close");

function seltst() {
  var kite = document.getElementById("poptext");
  var closebtn = document.getElementById("close");
  kite.style.display = 'block';
  setTimeout(el, 2000);
  kite.style.width = "500px";
}

function el() {
  kite.style.display = 'block';
}

function closepop() {
  kite.style.display = "none";
}

Take a look at the HTML code as well:

<p>
  <input readonly type="text" value="a random value" id="tbox" onselect="seltst()">
</p>
<div id="poptextcont">
  <div id="poptext">
    <span id="close" onclick="closepop()">&times;</span>
    <p id="poptexttwo">
      lots of text about stuff<a href="aboutus.html">Contact us</a> more text!
    </p>
  </div>
</div>

Please provide answers using only pure JavaScript.

Answer №1

Upon closer inspection, an abundance of unnecessary code was identified. After a thorough cleanup process, the following streamlined version emerged:

Simply click to hide the poptext, and upon selection (highlighting), reveal the poptext.

Furthermore, ensure that your script is positioned just before the closing </body> tag. It should come after all other HTML elements. In my illustration, if you were to move the script above the p, it would cease to function properly. Why is this the case?

The issue arises because, during page load, the command

var kite = document.getElementById("poptext");
is executed while the element has not yet loaded.

<p>
  <input readonly type="text" value="a random value" id="tbox" onselect="seltst()">
</p>
<div id="poptextcont">
  <div id="poptext">
    <span id="close" onclick="closepop()">&times;</span>
    <p id="poptexttwo">
      lots of text about stuff<a href="aboutus.html">Contact us</a> more text!
    </p>
  </div>
</div>
<script>
  var kite = document.getElementById("poptext");

  function seltst() {
    kite.style.display = 'block';
    kite.style.width = "500px";
  }

  function closepop() {
    kite.style.display = "none";
  }
</script>

Implementing this approach will yield success. By defining kite within the function, the element will be present when the function is invoked, ensuring seamless operation:

<script>
  function seltst() {
    var kite = document.getElementById("poptext");
    kite.style.display = 'block';
    kite.style.width = "500px";
  }

  function closepop() {
    var kite = document.getElementById("poptext");
    kite.style.display = "none";
  }
</script>
<p>
  <input readonly type="text" value="a random value" id="tbox" onselect="seltst()">
</p>
<div id="poptextcont">
  <div id="poptext">
    <span id="close" onclick="closepop()">&times;</span>
    <p id="poptexttwo">
      lots of text about stuff<a href="aboutus.html">Contact us</a> more text!
    </p>
  </div>
</div>

Answer №2

The kite variable is globally defined, but it is not available in the function you are attempting to use. This could be problematic since it appears to be declared before the document object model (DOM) has finished loading.

To resolve this issue, redeclare the kite variable within the closePop function.


function closepop() {
   var kite = document.getElementById("poptext");
   kite.style.display = "none";
}

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

Setting up multiple RabbitMQ or other backend servers within Node configurations

As someone working in DevOps, I have encountered a situation where our developers are claiming that the Node.js software they wrote can only point to a single backend server due to Node.js limitations. This assertion seems unbelievable to me. How is it eve ...

integrating a ui-bootstrap modal in a sidebar using angularjs

I recently utilized the modal example from https://angular-ui.github.io/bootstrap/ and everything worked perfectly. By using the code snippet below, I was able to successfully open the modal as shown in the example: $scope.open = function (size) { v ...

Revise the cascading style sheets for HTML content loaded via AJAX

I have a situation where I am dynamically loading HTML content using AJAX with JQuery. After the content is loaded, I am triggering a click event on specific elements within the newly added HTML. However, I am encountering an issue when trying to set the ...

Guide on attaching event handlers to a standard button template using Vue JS

Just diving into Vue.js and I have a specific requirement. I need to attach events to a generic button template. I attempted to do so using the following approach. Vue.component('ac-btn', { props: [ 'clickevent' ] ...

Getting the ID of a button: A step-by-step guide

My query involves a file named Index.aspx connected to another file called seatbooks.js. Within Index.aspx, there is a button with the id Indexbutton. This button has an eventonclick='book_ticket()' attribute. The book_ticket() method is includ ...

Adding and removing/hiding tab-panels in Angular 4 with PrimeNg: A step-by-step guide

I'm currently working on a tabView project with a list of tab-panels. However, I am struggling to find a way to dynamically hide and unhide one of the tab panels based on specific runtime conditions. Does anyone have any suggestions or insights on how ...

Removing data from every page of a table with the Datatable plugin

Currently, I am facing an issue with a table of data that I am handling using the Datatable plugin. My goal is to clear the table upon clicking a button and then redraw it with updated values received via an ajax call. The problem arises when trying to del ...

JavaScript code that iterates through all files in a designated folder and its subfolders using a for loop

I am looking to combine two JavaScript scripts into one, but I'm not sure how to do it. The first script uploads files from a specified folder to VirusTotal for scanning and returns the scan result. The second script lists all files in the specified ...

Receiving an undefined value of x in AJAX and web API

In the Javascript code, I am attempting to display the listview of reviews when a user clicks on a movie. However, I am encountering errors in the console for the second and third movies - Batman and Avatar - stating that they are not defined. Additionally ...

CSS - Overlapping <a> elements when resized

My buttons have the following properties: background: #c6cdf2; border: 1px solid #8896e4; border-radius: 3px; padding: 6px 10px 3px 10px; When the page resizes, the buttons don't respect the padding and start overlapping each other. https://i.sstat ...

Error: Unable to access 'map' property of an undefined variable in NextJS while using getStaticPaths

Currently facing an issue with dynamic routing in my Next.js project. I have a dynamic page [id].js and am trying to fetch data from an API. const res = await fetch(`myAPI`); const resData = await res.json(); const paths = resData.data.map((r) =&g ...

Move anchor tags within HTML content produced from Markdown using Jekyll

I am facing an issue with the navigation bar on a website I am currently developing. The fixed navigation bar at the top is causing some alignment problems with anchor tags. While I know about the common solution to this issue, I am unsure of how to implem ...

Setting the value of a custom component property dynamically after the component has been rendered

I'm currently developing an Angular application and have a specific requirement to work on. I am using a custom component with 3 inputs, and I want to bind this custom component tag in the HTML of my page. <my-column [setInfo]="info" [dis ...

I am attempting to update the URL of an iframe dynamically, but I am encountering an issue: the Error message stating that an Unsafe value is being

Currently, I am attempting to dynamically alter the src of an iframe using Angular 2. Here is what I have tried: HTML <iframe class="controls" width="580" height="780" src="{{controllerSrc}}" frameborder="0" allowfullscreen></iframe> COMPONE ...

Utilizing prototype JS to create a dynamic floating widget or banner

I recently developed a custom script based on the original jQuery code found at this link: However, I encountered issues when attempting to implement it with prototype JS. Specifically, there seems to be accuracy problems with calculating the upper and lo ...

Dynamic CSS columns with a set width on the left and right sides

Dividing my background into two parts has been a challenge. Background Image #1 Endlessly Repeating Background Image #1 Background Image #2 Endlessly Repeating Background Image #2 In CSS, creating a background with multiple elements is not as simple as i ...

Tips for choosing a nested element within a div using CSS

I'm looking to target a specific child element within a div using CSS. Here's the HTML code I have: <div class='body'> <text>This is a text element</text> </div> Instead of adding a class or an ID, I want to ...

Trying to incorporate a PHP echo statement into the innerHTML of a button is ineffective

I have a piece of jQuery code that is not functioning as expected: $("#erase").click(function(){ $("#erase").attr("disabled", "disabled"); // Prevent double-clicking $(this).html("Erasing..."); $.post("erase.php", {target: $("#targ ...

What mechanisms do chat apps use to detect when a user sends a message and trigger a re-render of the chat

Do I need to implement websockets for my React chat app using MySQL? I'm facing an issue where the app doesn't re-render when I open it in a new tab and type a message. Should I consider using websockets for real-time updates, or are there any ot ...

Ways to retrieve the locale parameter from the URL in Next Js

For my Next Js application, I've successfully implemented multi language support using the next-i18next module. Everything is working smoothly. Below is the code for my NabBar component: const NavBar = ({...props}) => { const router = useRouter( ...