You could use jquery ui dialog, or, if you want to get more ...fancy, you could use fancybox:
$(document).ready(function() {
$("a#inline").fancybox({
'hideOnContentClick': true
});
});
.btn{
border: 1px solid #006;
color:#01ACEE;
font: bold 16px Tahoma;
border-radius:7px;
padding:4px;
}
<link rel="stylesheet" href="http://dodsoftware.com/sotests/fancy/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://dodsoftware.com/sotests/fancy/jquery.fancybox-1.3.4.js"></script>
<script type="text/javascript" src="http://dodsoftware.com/sotests/fancy/jquery.easing-1.3.pack.js"></script>
<script type="text/javascript" src="http://dodsoftware.com/sotests/fancy/jquery.mousewheel-3.0.4.pack.js"></script>
<a id="inline" class="btn" href="#data">My Button</a>
<div style="display:none">
<div id="data">
<img alt="" src="http://farm9.staticflickr.com/8366/8483546751_86494ae914_m.jpg"><br>
<h3>My Cool Title</h3>
<p>Put your cool item descrtiption here</p>
</div>
</div>
Answer from Wesley Smith on Stack OverflowYou could use jquery ui dialog, or, if you want to get more ...fancy, you could use fancybox:
$(document).ready(function() {
$("a#inline").fancybox({
'hideOnContentClick': true
});
});
.btn{
border: 1px solid #006;
color:#01ACEE;
font: bold 16px Tahoma;
border-radius:7px;
padding:4px;
}
<link rel="stylesheet" href="http://dodsoftware.com/sotests/fancy/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://dodsoftware.com/sotests/fancy/jquery.fancybox-1.3.4.js"></script>
<script type="text/javascript" src="http://dodsoftware.com/sotests/fancy/jquery.easing-1.3.pack.js"></script>
<script type="text/javascript" src="http://dodsoftware.com/sotests/fancy/jquery.mousewheel-3.0.4.pack.js"></script>
<a id="inline" class="btn" href="#data">My Button</a>
<div style="display:none">
<div id="data">
<img alt="" src="http://farm9.staticflickr.com/8366/8483546751_86494ae914_m.jpg"><br>
<h3>My Cool Title</h3>
<p>Put your cool item descrtiption here</p>
</div>
</div>
You can try jquery ui dialog like,
HTML
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
SCRIPT
$(function() {
$( "#dialog" ).dialog();
});
Also you can try bootstrap modal
You basically have three ways:
By hand: make the popup div floating and position it absolutely, then set top to 100% (this will make it look like a popup menu. Make sure the parent element has relative positioning.
HTML:
<div class="input-group" style="position: relative;"> <div class="SearchCities"> ... </div> </div>CSS:
.SearchCities { float: right; position: absolute; top: 100%; }JS: No change required
Use jQueryUI's dialog plugin: as pointed out in nrsharma's answer you can use a jQuery plugin to make it look like a popup dialog.
$('.SearchCities').dialog({ autoOpen: false }); // Initialize dialog plugin $('.SearchCities').dialog("open"); // Open popupAPI Reference: http://api.jqueryui.com/dialog/
Examples: http://jqueryui.com/dialog/
Plugin download: http://jqueryui.com/download/
Use Bootstrap's dropdown component: it seems from your code that you're using bootstrap. You can easily use bootstrap dropdowns or modals.
Bootstrap modals are popup dialogs just like jQueryUI's dialog plugin, but they look better and they're much more customizable. Have a look there for an example: http://getbootstrap.com/javascript/#modals
Bootstrap dropdowns are simple dropdown menus, but with a bit of hacking you can put anything inside them.
All you need is a bit of HTML (no JavaScript):
<div class="input-group dropdown"> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" id="dropdownBtn" role="button">...</button> <div class="dropdown-menu SearchCities" role="menu"> ... </div> </div>To make this work you also need to include Bootstrap js plugins (bootstrap.js/bootstrap.min.js).
Reference: http://getbootstrap.com/javascript/#dropdowns
Here you can do this easily
HTML
<div class="dialogbox"> This is Dialogue box</div>
<a id="click">click here</a>
CSS
.dialogbox{width:100px; height:100px; background:grey;display:none}
JQUERY
<script src="latest jquery library"></script>
<script>
$(document).ready(function(){
$("#click").click(function(){
$(".dialogbox").toggle();
});
});
</script>
Here is a fiddle that actually does what you want - http://jsfiddle.net/WGPhG/6/
JS
function popUp(){
var popup = document.createElement('div');
popup.className = 'popup';
popup.id = 'test';
var cancel = document.createElement('div');
cancel.className = 'cancel';
cancel.innerHTML = 'close';
cancel.onclick = function (e) { popup.parentNode.removeChild(popup) };
var message = document.createElement('span');
message.innerHTML = "This is a test message";
popup.appendChild(message);
popup.appendChild(cancel);
document.body.appendChild(popup);
}
NOTES
To set the class on an element you use element.className instead of element.class. For the onclick event handler on the cancel element, it is better to directly assign the onclick handler with an anonymous function that does what you need as in my example above.
EDIT (More Efficient Way)
This is actually a much better of getting the results that you want because there is no overhead involved with recreating the elements every time the popup is shown. Fiddle - http://jsfiddle.net/WGPhG/7/
CSS
.popup
{
position:absolute;
top:0px;
left:0px;
margin:100px auto;
width:200px;
height:150px;
font-family:verdana;
font-size:13px;
padding:10px;
background-color:rgb(240,240,240);
border:2px solid grey;
z-index:100000000000000000;
display:none
}
.cancel
{
display:relative;
cursor:pointer;
margin:0;
float:right;
height:10px;
width:14px;
padding:0 0 5px 0;
background-color:red;
text-align:center;
font-weight:bold;
font-size:11px;
color:white;
border-radius:3px;
z-index:100000000000000000;
}
HTML
<button onClick="openPopup();">click here</button>
<div id="test" class="popup">
This is a test message
<div class="cancel" onclick="closePopup();"></div>
</div>
JS
function openPopup() {
document.getElementById('test').style.display = 'block';
}
function closePopup() {
document.getElementById('test').style.display = 'none';
}
Try this update on JSFiddle
Changed :
- Center page (fixed).
- effect (fadein and fadeout)
HTML
<button onClick="openPopup();">click here</button>
<div id="test" class="popup" style="display:none;">
This is a test message
<div class="cancel" onclick="closePopup();"></div>
</div>
CSS
.popup {
position:fixed;
top:0px;
left:0px;
bottom:0px;
right:0px;
margin:auto;
width:200px;
height:150px;
font-family:verdana;
font-size:13px;
padding:10px;
background-color:rgb(240,240,240);
border:2px solid grey;
z-index:100000000000000000;
}
.cancel {
display:relative;
cursor:pointer;
margin:0;
float:right;
height:10px;
width:14px;
padding:0 0 5px 0;
background-color:red;
text-align:center;
font-weight:bold;
font-size:11px;
color:white;
border-radius:3px;
z-index:100000000000000000;
}
.cancel:hover {
background:rgb(255,50,50);
}
JS
function openPopup() {
//document.getElementById('test').style.display = 'block';
$('#test').fadeIn(1000);
}
function closePopup() {
//document.getElementById('test').style.display = 'none';
$('#test').fadeOut(500);
}
Give an ID to uniquely identify the button, lets say myBtn
// when DOM is ready
$(document).ready(function () {
// Attach Button click event listener
$("#myBtn").click(function(){
// show Modal
$('#myModal').modal('show');
});
});
JSFIDDLE
Below mentioned link gives the clear explanation with example.
http://www.aspsnippets.com/Articles/Open-Show-jQuery-UI-Dialog-Modal-Popup-on-Button-Click.aspx
Code from the same link
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/blitzer/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(function () {
$("#dialog").dialog({
modal: true,
autoOpen: false,
title: "jQuery Dialog",
width: 300,
height: 150
});
$("#btnShow").click(function () {
$('#dialog').dialog('open');
});
});
</script>
<input type="button" id="btnShow" value="Show Popup" />
<div id="dialog" style="display: none" align = "center">
This is a jQuery Dialog.
</div>
DEMO
In the content area you can provide whatever you want to display in it — using the following code from https://gist.github.com/westonwatson/3914027 that I have copied in here:
.black_overlay {
display: none;
position: absolute;
top: 0%;
left: 0%;
width: 100%;
height: 100%;
background-color: black;
z-index: 1001;
-moz-opacity: 0.8;
opacity: .80;
filter: alpha(opacity=80);
}
.white_content {
display: none;
position: absolute;
top: 25%;
left: 25%;
width: 50%;
height: 50%;
padding: 16px;
border: 16px solid orange;
background-color: white;
z-index: 1002;
overflow: auto;
}
<html>
<head>
<title>LIGHTBOX EXAMPLE</title>
</head>
<body>
<p>This is the main content. To display a lightbox click <a href="javascript:void(0)" onclick="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">here</a>
</p>
<div id="light" class="white_content">This is the lightbox content. <a href="javascript:void(0)" onclick="document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'">Close</a>
</div>
<div id="fade" class="black_overlay"></div>
</body>
</html>
You can simply use jQuery UI Dialog
Example:
$(function() {
$("#dialog").dialog();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery UI Dialog - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
</head>
<body>
<div id="dialog" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
</body>
</html>
You can use jQuery UI like this:
JS:
$(function(){
$('.profile1').on("click", function(e){
e.preventDefault()
$('.picture').dialog({
width: 600,
height: 'auto',
modal:true,
title: 'Picture',
overlay: { backgroundColor: "#000", opacity: 0.9 }
})
})
})
HTML:
<div class="box">
<div class="box"> <div class="profile1">
<p class="name">NAME</p>
</div>
<div class="picture">Photo Here...</div>
</div>
CSS for ".picture":
.picture{
display:none;
width:30%;
height:30%;
}
And don't forget to include jQuery UI in addition to jQuery and the jQuery UI CSS:
jQuery: <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
jQuery UI:<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
CSS: https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.min.css(you should download this CSS, not link to CDN)
JSFiddle: https://jsfiddle.net/91k8xa22/
Tell me if I can improve it :)
https://jsfiddle.net/www139/x6q7Ls5g/
window.addEventListener('load', function() {
var profileBox = document.getElementsByClassName('profile1')[0];
var picture = document.getElementsByClassName('picture')[0];
profileBox.addEventListener('click', function() {
document.getElementById('popupbox').style.display = 'block';
picture.innerHTML = '<img src="http://freetopwallpaper.com/wp-content/uploads/2013/09/beautiful-background-wallpaper-hd-3.jpg">';
});
});
.box {
left: 19.5%;
top: 11.5%;
height: 85%;
right: 2.2%;
background-color: #F2F2F2;
border-radius: 5px;
position: absolute;
border: 1px soid F2F2F2(0, 0, 0, 0.3);
z-index: -1;
overflow: auto;
}
.profile1 {
margin-left: 1.7%;
margin-top: 6%;
height: 40%;
width: 18%;
background-color: #0D7BFF;
position: absolute;
z-index: -1;
border-radius: 2px;
}
#popupbox {
position: fixed;
width: 50vw;
height: 50vh;
background-color: #eee;
display: none;
}
<div class="box">
<div class="profile1">
<div class="picture"></div>
<p class="name">NAME</p>
</div>
</div>
<!--popup box-->
<div id="popupbox">This is the popup box</div>
<!--end popup box-->
Not sure what you want exactly as your output, but the correct way of doing the code is as below:
<h2>Popup</h2>
<div class="popup" onclick="myFunction('Harry Potter','Wizard')">
<button> Try it </button>
<span class="popuptext" id="myPopup">Ankit </span>
</div>
<p id="demo"></p>
<script>
// When the user clicks on div, open the popup
document.getElementById("myPopup").innerHTML= "Welcome " + name + ", the " + job + ".";
function myFunction(name,job) {
var popup =document.getElementById("myPopup");
popup.innerHTML = "Welcome " + name + ", the " + job + ".";
popup.classList.toggle("show");
}
</script>
With respect to this code,
On clicking the "Try it" button, you will get a pop-up as "Welcome Harry Potter, the Wizard. ". The reason being you passed the values "Harry Potter" as name and "Wizard" as job
onclick="myFunction('Harry Potter','Wizard')"
Now, in the function, you declared a variable named popup which stores the value from id "myPopup", which is in this case is "Ankit" right now
In the next line, we are changing the value to be "Welcome " + name + ", the " + job + ".", turning the line to be "Welcome Harry Potter, the Wizard". The value "Ankit" is lost.
popup.classList.toggle("show"); simply creates the pop-up.
If in case you wish to have "Ankit" as well in the pop-up, you may wish to try this snippet:
<div class="popup" onclick="myFunction('Harry Potter','Wizard')">
<button> Try it </button>
<span class="popuptext" id="myPopup"> Hi, I am Ankit. </span>
</div>
<p id="demo"></p>
<script>
// When the user clicks on div, open the popup
document.getElementById("myPopup").innerHTML= "Welcome " + name + ", the " + job + ".";
function myFunction(name,job) {
var popup =document.getElementById("myPopup");
popup.innerHTML = popup.innerHTML + " Welcome " + name + ", the " + job + ".";
popup.classList.toggle("show");
}
</script>
The popup will now show you as "Hi, I am Ankit. Welcome Harry Potter, the Wizard".
Hope this helps in some way :)
Try this code, i tried it myself and it worked
<div class="popup" onclick="myFunction('some text')">Click me to toggle the popup!
<span class="popuptext" id="myPopup"></span>
</div>
<script>
// When the user clicks on div, open the popup
function myFunction(value) {
var popup = document.getElementById("myPopup");
popup.innerText = value;
popup.classList.toggle("show");
}
</script>