JQuery is used to create a pop-up box

As a newcomer to JQuery, I am working on implementing a pop-up box on my webpage.
Utilizing a jQuery plugin derived from Ray Stone's leanModal:

(function($) {
  $.fn.extend({
    leanModal: function(options) {
      var defaults = {
        top: 100,
        overlay: 0.5,
        closeButton: null
      };
      var overlay = $("<div id='lean_overlay'></div>");
      $("body").append(overlay);
      options = $.extend(defaults, options);
      return this.each(function() {
        var o = options;
        $(this).click(function(e) {
          var modal_id = $(this).attr("href");
          $("#lean_overlay").click(function() {
            close_modal(modal_id)
          });
          $(o.closeButton).click(function() {
            close_modal(modal_id)
          });
          var modal_height = $(modal_id).outerHeight();
          var modal_width = $(modal_id).outerWidth();
          $("#lean_overlay").css({
            "display": "block",
            opacity: 0
          });
          $("#lean_overlay").fadeTo(200, o.overlay);
          $(modal_id).css({
            "display": "block",
            "position": "fixed",
            "opacity": 0,
            "z-index": 11000,
            "left": 50 + "%",
            "margin-left": -(modal_width / 2) + "px",
            "top": o.top + "px"
          });
          $(modal_id).fadeTo(200, 1);
          e.preventDefault()
        })
      });

      function close_modal(modal_id) {
        $("#lean_overlay").fadeOut(200);
        $(modal_id).css({
          "display": "none"
        })
      }
    }
  })
})(jQuery);

The CSS associated with this setup is as follows:

#lean_overlay {
  position: fixed;
  z-index:100;
  top: 0px;
  left: 0px;
  height:100%;
  width:100%;
  background: #000;
  display: none;
}
#signup {
  width: 404px;
  padding-bottom: 2px;
  display:none;
  background: red;
  border-radius: 5px;
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  box-shadow: 0px 0px 4px rgba(0,0,0,0.7);
  -webkit-box-shadow: 0 0 4px rgba(0,0,0,0.7);
  -moz-box-shadow: 0 0px 4px rgba(0,0,0,0.7);
}

The initialization script used for this purpose is:

<script type="text/javascript">
  $(function() {
    $("#signup").leanModal();
  });
</script>

And, here is the HTML structure being implemented:

<p id="examples" class="section box">
  <strong>Example:</strong>
  <a id="go" rel="leanModal" name="signup" href="#signup">With Close Button</a>
</p>

<div id="signup">
  <div id="signup-ct">
    <div id="signup-header">
      <h2>This is a test</h2>
      <p>testing.</p>
      <a class="modal_close" href="#"></a>
    </div>
  </div>
</div>

Despite all of these components in place, I am facing difficulty getting the pop-up to display properly. Any suggestions or assistance would be greatly appreciated!

Answer №1

While most people recommend using the existing library, if you prefer to tackle the issue with your own code, here's where I believe the mistake lies:

You may be targeting the wrong element in your 'leanmodal' call.

To demonstrate a solution, I've set up a jsFiddle that displays the desired dialog when the link is clicked. Check it out here: http://jsfiddle.net/pRJmQ/

In essence, I have made a tweak to this line of code:

$("#signup").leanModal();

Now, it looks like this:

$('#go').click(function() { 
    $(this).leanModal(); 
});

Answer №2

One effective approach is to utilize the jQuery UI, particularly the modal plugin which can be downloaded separately if you only require that functionality.

Thanks

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

Exploring simpleML's capability to delve deeper into the depths of the page source

I've been able to successfully extract the username babesmcphee from reading this site using simpleML in processing. However, I'm facing an issue with retrieving the second username (JohnCMayer) and subsequent ones. I suspect the problem lies in ...

Tips for managing local storage asynchronously

I have two files in my TypeScript application, namely: File 1 and File 2, In File 1, I want to save a value in local storage like this: private load() { return this.entityService .load(this.$scope.projectRevisionUid) ...

Center the middle item in a flexbox column arrangement

I'm working on a layout where I have three items stacked vertically within a column. My goal is to center the middle item (green) in the column and have the other items positioned around it. Here's my current setup: .flex-row { display: fl ...

Ways to deactivate a button using CSS

I need to use Javascript to disable a link based on its id. By default, the link is invisible. I will only enable the link when the specific id value comes from the backend. HTML <li id="viewroleId" style="display: none;"> <a href="viewrole" ...

Unable to render Angular charts

Recently, I've been attempting to integrate angular charts using the chart.js bundle sourced from CDN links. The version of Angular being used is 1.5.7 and for chart.js it's 2.1.6. I encountered the following error message: angular.js:13708 T ...

Tips on deleting the last character in an input field with AngularJS

Can you help me with the title of this question? I'm currently working on developing a straightforward calculator using AngularJS. It's operational at the moment, but I'm looking to incorporate additional buttons like a "delete" key and a de ...

What is the title of this particular CSS method?

I've been implementing a unique approach for more than a year now, and I have yet to come across similar practices elsewhere. Essentially, I am structuring display "states" or "modes" by utilizing CSS classes. Despite searching for terms like "css mod ...

Varying outcomes in mongo due to specific field uniqueness

Recently, I've noticed issues with consistently fetching items from my mongo database. With over 4000 items stored in the database, I've encountered some unexpected behavior. Here's a glimpse at the schema: var Order = new Schema({ code: { ...

Scrollable tabs with Ionic

Hey there! I was researching before starting my project to see if Ionic has a feature similar to scrollable tabs. I noticed this in the fitrpg app, which was built with Ionic, but I'm unsure if it's a custom design. I plan to use it for a list si ...

After the submit button is disabled, the form fails to be submitted

Hello, I am having an issue with disabling my button after the form is submitted. The button gets disabled, but the PHP code does not execute. I have tried various scripts from the internet, but they all seem to have the same result: the button gets disab ...

"Troubleshooting: Click counter in HTML/Javascript unable to function

My latest HTML project is inspired by the "cookie clicker" game, and I'm working on it for a friend. So far, I've managed to get the click counter to function to some extent. Essentially, when you click on the image, the number at the bottom sho ...

How to place a Div element in the middle of a webpage

My webpage background features an image of old Tbilisi, and here is the code I am using: <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <style> #links { margin: 0 auto; ...

Building a class structure with a JavaScript MVC approach - best practices

I am looking to develop a JavaScript web application using the MVC-like principle. While my code is functional, I am facing challenges in implementing it correctly. I have created a global variable called APP where I store controllers, views, and other com ...

How can I target specific HTML comments using XPath?

I need to extract specific nodes from a large HTML page by using Xpath: <html> ........ <!-- begin content --> <div>some text</div> <div><p>Some more elements</p></div> <!-- end content --> ....... ...

The XMLHttpRequest is unable to load.... The origin null has been restricted by Access-Control-Allow-Origin

I'm having trouble printing a JSON from a broker on my server. I am unable to return the JSON as Chrome gives me the following error: XMLHttpRequest can not load .... Origin null is not allowed by Access-Control-Allow-Origin. Can anyone help me wi ...

Exploring the intricate design and information dynamics of MRAID 2

I am currently in the process of developing a new MRAID (v2) compliant SDK for Android that will enable rich media ads to be displayed within various android apps. Additionally, I plan to implement a backend platform that allows advertisers to create their ...

Vertical text alignment alongside an image in multiple lines

I have been struggling with aligning multi-line text next to an image in a responsive design. No matter what I try, when the browser window is resized and the text becomes multi-lined, it always falls below the image positioned to the left of it. Can any ...

Step-by-step guide to automatically submitting a form after obtaining geolocation without the need for manual button-clicking

I am looking for a way to automatically call a page to get geolocation data, and then submit the form to my PHP page with the values of getlat and getlon without the need to click a submit button. I have tried implementing the code below, however, despit ...

Guide to creating a select box connected to a range slider in VueJS

Hey there! I'm a beginner in Vue and currently working on a project that involves Vue, PHP, and Laravel. Here is a picture link showing the price range filter that I need to implement. I'm struggling with coding the select box so that its value ...

Node.js provides a straightforward way to decode HTML code

Can strings with HTML-encoded characters like &#39;, &amp;, etc., be converted into plain character strings without using multiple str.replace(/&#39;/g, "'") statements? ...