Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html Modal popup

How to make a simple modal popup for the following code.And on click on the background the modal popup should not disappear.

<html>
<input type="textarea"></input>
</html>
like image 738
Hulk Avatar asked Jan 02 '10 17:01

Hulk


People also ask

How do you add a modal in HTML?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do I create a popup dialog box in HTML?

In this article, we will create a dialog box or window using the <dialog> tag in the document. This tag is used to create popup dialog and models on a web page. This tag is new in HTML5.

What is modal tag in HTML?

A modal is a dialog box/popup window that is displayed on top of the current page: Open Modal.


2 Answers

Here's a plain-JavaScript example:

var modal = document.getElementById('modal');
var shade = document.getElementById('shade');
document.getElementById('start').onclick = function() {
  modal.style.display = shade.style.display = 'block';
};
document.getElementById('close').onclick = function() {
  modal.style.display = shade.style.display = 'none';
};

// This code is a workaround for IE6's lack of support for the
// position: fixed style.
//
if (!('maxHeight' in document.body.style)) {
  function modalsize() {
    var top = document.documentElement.scrollTop;
    var winsize = document.documentElement.offsetHeight;
    var docsize = document.documentElement.scrollHeight;
    shade.style.height = Math.max(winsize, docsize) + 'px';
    modal.style.top = top + Math.floor(winsize / 3) + 'px';
  };
  modal.style.position = shade.style.position = 'absolute';
  window.onscroll = window.onresize = modalsize;
  modalsize();
}
body {
  margin: 0;
}

#shade,
#modal {
  display: none;
}

#shade {
  position: fixed;
  z-index: 100;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

#modal {
  position: fixed;
  z-index: 101;
  top: 33%;
  left: 25%;
  width: 50%;
}

#shade {
  background: silver;
  opacity: 0.5;
  filter: alpha(opacity=50);
}
<div id="shade"></div>
<div id="modal">
  <textarea rows="5" cols="25"></textarea>
  <button id="close">Close</button>
</div>

<p>
  <button id="start">Start</button>
</p>

There are various improvements you can make from there, such as iframe hacks to fix IE z-indexing, or encapsulating it in a reusable object, but that's the basic way it's done.

like image 158
bobince Avatar answered Oct 14 '22 12:10

bobince


you can also use native HTML5.1 dailog. currently dialog element is only supported in Chrome 37+, Safari 6+ and Opera 24+.

var dailog = document.getElementById("dialog"); 

function openModal() { 
   // dailog.show(); 
      dailog.showModal();
} 

function closeModal() { 
    dailog.close(); 
} 
#dialog{width:300px;}
.right{float:right}
<button onclick="openModal()">Show dialog</button>


<dialog id="dialog">This is a dialog window<br/><br/><br/>
<button onclick="closeModal()" class="right">Close</button>
</dialog>
like image 26
Danish Khan Avatar answered Oct 14 '22 10:10

Danish Khan