I am trying to hide the popup if the background is clicked, but NOT the div.
Basically, when the user clicks the background it will hide the div; yet, if the user clicks the actual div it will still hide it. I would only like the div to be hidden on the clicking of the background.
Here is my code:
HTML
<div id="linkinputholder">
<div id="linkinputbox">
Title
</div>
</div>
<button onclick="displaylinkinput()" type="button"> Display </button>
CSS
#linkinputholder {
display: none;
position: fixed;
z-index: 100;
width: 100%;
min-height: 100%;
left: 0px;
top: 0px;
background: rgba(0, 0, 0, 0.2);
}
#linkinputbox {
display: block;
background-color: red;
width: 500px;
height: 100px;
position: fixed;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
JS/Jquery
function displaylinkinput() {
document.getElementById('linkinputholder').style.display = "block";
}
$('#linkinputholder').click(function() {
document.getElementById('linkinputholder').style.display = "none";
});
I'm assuming by background you mean your linkinputholder div, which is 100% wide by 100% tall. Your jquery code was missing the call to displaylinkinput, so i added a click event handler to call it. When you click on the linkinputbox div, the click event passes down through to linkinputholder. To prevent this just stop the event propagation.
$('#linkinputbox').click(function (evt) {
evt.stopPropagation();
});
I have created a JSFIDDLE for you here: http://jsfiddle.net/seadonk/oLgex1pq/
Here is the corrected javascript:
function displaylinkinput() {
$('#linkinputholder').show();
}
$(function () {
$('button').click(function () {
displaylinkinput();
});
$('#linkinputholder').click(function () {
$('#linkinputholder').hide();
});
$('#linkinputbox').click(function (evt) {
evt.stopPropagation();
});
})();
Edit
Check if div is target
$('#linkinputholder').click(function(event) {
if (jQuery(event.target).is('.linkinputholder')) return;
document.getElementById('linkinputholder').style.display = "none";
});
Related
The scenario is as follows.
Default Status (no layer popup)
When I click the button, layer popup shows.
Click the button or outside, layer popup will be hide.
I want to close the layer popup when I click background(outside) or button.
How can I do with Vanilla JS or jquery? (based on HTML)
I would appreciate it if you could answer.
When you open the popup attach a click listener to body that closes it and removes the listener.
You can use this code
//use by id
document.getElementById(#id).style.display = 'block';
document.getElementById(#id).style.display = 'none';
//use by className
document.getElementById(.className).style.display = 'none';
document.getElementById(.className).style.display = 'block';
or use jQuery
$(document).ready(function(){
$("#id").click(function(event){
// $("#id").toggle();
// $("#id").hide();
// $("#id").show();
});
});
Set id for your layer in HTML part like id="layerPopup"
Then on your JS code create event for your button
$(document).on('click', '#btnId', function(){
$("#layerPopup").hide();
});
You should appear a overlay which will cover the whole body, and give it css property z-index to lower from the button, and when apply click function on it same as my code
HTML
<div class="overlay"></div>
CSS
.overlay{
background-color: transparent;
inset: 0;
position: fixed;
z-index: 100;
display: none;
}
button{
z-index: 101;
}
JQuery
$('button').click(function(){
$('.overlay, popup').toggle();
});
$('.overlay').click(function(){
$('.overlay, popup').hide();
});
One standard way to handle such scenario is to have a backdrop div behind the popup and then add an event listener to it. You may choose to change backdrop's background color to increase pop up aesthetics visibly.
.backdrop {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
z-index: 10;
background: rgba(0, 0, 0, 0.75);
}
.modal {
position: fixed;
top: 30vh;
left: 10%;
width: 80%;
z-index: 100;
overflow: hidden;
}
<div class="backdrop" />
<div class="modal" />
And then you can add an event listener on backdrop:
$(document).on('click', '.backdrop', function(){
$(".modal").hide();
});
PS: There may be some syntax issues!
I just need to have a modal close on click off of it. I tried 2 approaches:
Targeting a click event on body and check if the modal has a class and if it does show it
check the event.target and if it's not the modal hide it
Two attempts are below:
$(function(e) {
$("#filter-button").click(function(e) {
$(".dialog").toggleClass("show");
});
$("body").click(function() {
if ($(".dialog").hasClass("show")) {
$(".dialog").removeClass("show");
}
});
});
.dialog {
display: none;
width: 300px;
height: 300px;
background-color: red;
}
.show {
display: block !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="filter-button">SHOW/HIDE</button>
<div class="dialog"></div>
Upon click on "SHOW/HIDE" the modal (a red box) does not even open. I think this might have something to do with #filter-button being counted as a target? As a troubleshooting initiative for the above sample, I attempted to use e.currentTarget https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget so basically changing the above to:
$(function(e) {
console.log(e.currentTarget);
...
I got nothing in the console so I can't tell if that's the issue.
I also tried to log e.target and got no results in the console as well.
Why is that?
My next attempt:
$(function(e) {
$("#filter-button").click(function(e) {
$(".dialog").toggleClass("show");
});
if(e.currentTarget != $("#filter-button")) {
$(".dialog").removeClass("show");
}
});
.dialog {
display: none;
width: 300px;
height: 300px;
background-color: red;
}
.show {
display: block !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="filter-button">SHOW/HIDE</button>
<div class="dialog"></div>
The toggle function is restored, but clicking off of the modal does not close it. I found: Check if event target is hyperlink so I changed my code to:
$(function(e) {
$("#filter-button").click(function(e) {
$(".dialog").toggleClass("show");
});
if(e.target.tagName.toLowerCase() === 'body') {
$(".dialog").removeClass("show");
}
});
This breaks my previous code again, and now my dialog doesn't open at all.
howcome I can't console log e.target?
Why is the modal not opening at all in the first example? Is it because of a logic error with targeting body somehow?
Which is the better way? e.target or attaching a click event to the body?
To simplify things you could wrap your modal dialogue within a container that is full width and height of the viewport. This way you can show or hide the parent container if it is clicked instead of showing and hiding just the dialogue.
This also allows you to add an overlay with css later on to increase the visibility of the modal.
$(function(e) {
var modal = $(".modal-wrapper")
$("#filter-button").click(function(e) {
modal.toggleClass("show");
});
$(window).click(function(e) {
if (e.target == modal[0]) {
modal.removeClass("show");
}
});
});
.modal-wrapper {
display: none;
width: 100vw;
height: 100vh;
position: absolute;
top: 0;
left: 0;
}
.modal {
width: 300px;
height: 300px;
background-color: red;
}
.show {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="filter-button">SHOW/HIDE</button>
<div class="modal-wrapper">
<div class="modal"></div>
</div>
I have a div assigned to trigger some Javascipt which opens a 'PopUp' RSVP form on my site,I however also want to have a black background full screen overlay when the window pops up to hide the rest of the site.
The popup window works fine using this:
$(function() {
// contact form animations
$('#contact').click(function() {
$('#contactForm').fadeToggle();
});
$(document).mouseup(function (e) {
var container = $("#contactForm");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.fadeOut();
}
});
So I want to attach the overlay action into this function so that it toggles on and off with the PopUpWindow. I already have a div assigned with the correct CSS,I just need to get it to also toggle:
.sidebar-overlay {
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
background-color: rgba(0,0,0,1);
position: fixed;
z-index: 3;
display: none;
cursor: e-resize;
}
I am a little stuck so any help is very much appreciated. Thanks
I think in your script, you checked for the contact form being clicked or not, but then you hide only the form, and not the sidebar-overlay section. So it's really just a slight mix up in which selector is in use. (I can't be positive if that's what's going on, as you did not include the html markup in your question.)
Here's a quick example with your code and the matching html. Only, the sidebar-overlay is hid on click of the document area.
$('#contact').click(function() {
var container = $(".sidebar-overlay");
container.fadeToggle();
});
$(document).mouseup(function (e) {
var container = $(".sidebar-overlay");
var contactform = $("#contactForm");
if (!contactform.is(e.target) // if the target of the click isn't the container...
&& contactform.has(e.target).length === 0) // ... nor a descendant of the container
{
container.fadeOut();
}
});
.sidebar-overlay {
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
background-color: rgba(0,0,0,1);
position: fixed;
z-index: 3;
display: none;
cursor: e-resize;
}
#contactForm{
width:50%;
margin:20px auto;
padding:20px;
background:pink;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="contact">click</button>
<div class="sidebar-overlay">
<form id="contactForm">
This is my form<br />
field 1 ______<br />
field 2 ______<br />
</form>
</div>
I am trying to hide a div using
if ($("input,textarea").is(":focus")) {
$("#logos").hide();
} else {
$("#logos").show();
}
I have also tried
if ($("#input").is(":focus")) { }
and giving the input id of "input", but it seems to not work. It's supposed to work like in mobile google search.
What am I missing here?
You're probably missing an event handler
$("input,textarea").on({
focus : function() {
$("#logos").hide();
},
blur : function() {
$("#logos").show();
}
});
Here is a working example, using the jQuery methods
show()
hide()
focus()
blur()
$(document).ready(function(){
$('.logos').hide();
$('.focus').focus(function(){
$('.logos').show();
});
$('.focus').blur(function(){
$('.logos').hide();
});
});
div, textarea {
position: absolute;
width: 100px;
height: 100px;
vertical-align: top;
}
.logos {
top: 0;
left: 0;
color: rgb(255,255,255);
background-color: rgb(255,0,0);
}
.focus {
top: 0;
left: 112px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="logos">
#logos
</div>
<textarea class="focus">
Focus on me
</textarea>
I am trying to display a pop up window over the parent window in my java web application. When the user clicks on a link in parent window a pop up window must appear over the parent. In the pop up window user can select any value being fetched from database(hibernate). After that when user clicks "OK" button inside the pop up window or clicks anywhere outside the pop up or in parent window that pop up shall hide.
Create a wrapper element that has a z-index superior to your parent window, but lower than your popup window.
addEventListener for "click" to that element.
If the target === that element, close the popup and remove the element itself.
That will handle your clicks "outside the popup".
The rest should be handled by your window's events.
EDIT
styles
html {
height: 100%;
}
body {
position: relative;
height: 100%;
z-index: 1;
}
#overlay {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
background: rgba(0,0,0,0.25);
z-index: 99;
}
#popup {
position: absolute;
width: 20%;
height: 20%;
top: 40%;
left: 40%;
background: rgb(220,220,220);
box-shadow: 2px 2px 3px rgba(0,0,0,0.5);
z-index: 100;
}
html
<input id="popupbutton" type="button" value="pop me up" />
javascript
<script>
document.getElementById('popupbutton').addEventListener('click', loadPopup, true);
function loadPopup(e) {
e.preventDefault();
e.stopPropagation();
var overlay = document.createElement('div');
overlay.id = 'overlay';
overlay.addEventListener('click', closePopup, true);
var popup = document.createElement('div');
popup.id = 'popup';
document.body.appendChild(overlay);
document.body.appendChild(popup);
function closePopup(e) {
e.preventDefault();
e.stopPropagation();
// only close everything if click was on overlay
if (e.target.id === 'overlay') {
document.body.removeChild(popup);
document.body.removeChild(overlay);
}
}
}
</script>
EDIT 2
Link to working JS fiddle
http://jsfiddle.net/md063bfr/1/
you can use div.
ex-
<td>
<div align="center" id="show_sub" style="background-color: pink;display:none;width:670px;height:370px;top:110px;overflow: auto;">
your content here
</div>
</td>
then set display none->show in javascript function
function ShowDiv(){
Popup.show('show_sub');
}
thats all.
thanx.