wait for click before losing focus - html

I have a search dropdown which shows suggestions when a search input gets focus.
The problem is I have code such that the dropdown closes when this input loses focus. This is how I want it but the problem is that as soon as I click on any suggestion, the box closes before the click is registered for that suggestion.
My code:-
html:-
<div id="demo-2">
<input
type="search"
placeholder="Search By Title, Author"
/>
<div class="autocomplete">
<mdb-card *ngFor="let book of books" (click)="logger(book.id)">
<!--some code-->
</mdb-card>
</div>
</div>
css:-
#demo-2 input[type="search"]:focus {
width: 275px;
color: #000;
background-color: #fff;
cursor: auto;
~ .autocomplete {
visibility: visible;
}
}
.autocomplete {
height: 350px;
width: 275px;
position: absolute;
visibility: hidden;
overflow: auto;
}
ts (excerpt):-
logger(id) {
console.log(id);
}
Been at it for more than a day. Any help is much appreciated.

Create a variable isVisible in your TS:
isVisible = false;
logger(id) {
console.log(id);
isVisible = false;
}
hide() {
isVisible = false;
}
inputClicked() {
isVisible = true;
}
And your HTML:
<div id="demo-2">
<input
type="search"
placeholder="Search By Title, Author" (focusout)="hide()"
/>
<div class="autocomplete" [ngStyle]="{'visibility':isVisible ? 'visible' : 'hidden'}">
<mdb-card *ngFor="let book of books" (click)="logger(book.id)">
<!--some code-->
</mdb-card>
</div>
</div>
Remove visibility property from your CSS.

~.autocomplete will apply properties on element's siblings. So rather than applying it on div, try to apply it on mdb-card

Related

how to have a dropdown activate when input is focused?

I'm trying to make a reddit clone MERN and need a dropdown like the dorpdown in the reddit search bar
the styling is not an issue the issue is to make the dropdown appear when the input bar is focused
also i am using tailwindcss
can anyone please tell me how to do this?
I suppose you want to show the select not just when you focus the input but also when you are interacting with the select: you may avoid JS at all and use the :focus-within pseudoclass
.dropdown select {
display: none;
}
.dropdown:focus-within select {
display: block;
}
<div class="dropdown">
<input />
<select>
<option>option</option>
</select>
</div>
but if you need this to be working only on input focus
.dropdown select {
display: none;
}
.dropdown input:focus + select {
display: block;
}
<div class="dropdown">
<input />
<select>
<option>option</option>
</select>
</div>
const show = () => {
document.querySelector('.content').classList.add("active")
}
const hide = () => {
document.querySelector('.content').classList.remove("active")
}
.dropdown{
display: flex;
flex-direction: column;
position: relative;
width: 200px;
}
input{
width: 100%;
}
.content{
background: red;
padding: 4px;
position: absolute;
top: 100%;
width: 100%;
display: none;
}
.active{
display: flex;
}
<div class = "dropdown">
<input onFocus = "show()" onBlur = "hide()" />
<div class = "content">
Dropdown
</div>
</div>
React and Tailwind
const Dropdown = () => {
const [isFocus, setIsFocus] = React.useState(false);
return (
<div className = "w-40 border m-4">
<input
className = "w-full"
onFocus={() => setIsFocus(true)}
onBlur={() => setIsFocus(false)}
/>
{ isFocus && <div className = "bg-red-300">Dropdown</div>}
</div>
);
};

How to place a drop-down and a div inside a div, so that it ll act as a dropdown?

I am really confuded in this. I have a label and select drop-down inside my container which is right aligned.
GOAL
Container should act like a drop-down. Only sort-by label should be displayed initially.When user clicks on it, it should shoe the option to the user.
Problem
I don't know how to trigger drop down when i click on the sort by label.
<div class="container">
<label class="Sortlabel">Sort by</label>
<select>
<option>2</option>
<option>22</option>
<option>222</option>
</select>
</div>
If i must use jquery or JS, i ll add these tags also. Any suggestions??
And what is the difference with this one : http://jsfiddle.net/csdtesting/vuc81u87/
Result is the same.
<div class="container">
<label class="Sortlabel"></label>
<select>
<option>Sort by</option>
<option>22</option>
<option>222</option>
</select>
</div>
select
{
width:100%;
-webkit-appearance: none;
}
.container
{
float: right;
width:190px;
}
But if you insists.I took this idea
and here it is (Pure Javascript): http://jsfiddle.net/csdtesting/ybjdsqrx/
var state = false;
// <select> element displays its options on mousedown, not click.
showDropdown = function(element) {
var event;
event = document.createEvent('MouseEvents');
event.initMouseEvent('mousedown', true, true, window);
element.dispatchEvent(event);
};
// This isn't magic.
window.runThis = function() {
var dropdown = document.getElementById('sel');
showDropdown(dropdown);
};
select {
-webkit-appearance: none;
border: none;
width: 70%;
}
.container {
width: 100%;
float: left;
width: 190px;
border: 1px solid;
}
.Sortlabel {
width: 20%;
}
}
<div class="container">
<label class="Sortlabel" onclick="runThis()">Sort by</label>
<select id="sel">
<option></option>
<option>2</option>
<option>22</option>
<option>222</option>
</select>
</div>

Prevent contenteditable element from getting focus when clicking parent

When clicking anywhere above the "Outside" div container in the following example, the contenteditable span element gets the focus.
<div style="margin:30px">
<span contenteditable="true" style="background:#eee">
Hello World
</span>
<div>Outside</div>
</div>
Here's a jsFiddle:
http://jsfiddle.net/AXM4Y/
I want the contenteditable behave more like a real text input, thus only clicking the span element itself should trigger its focus. How do I do this? How can the event capturing of the container element be stopped. I've already tried "e.stopPropagation()" with jQuery on the parent div, but it didn't work.
I fixed it with the way I put pointer-events: none on parent element and pointer-events: all on element with content editable attribute.
There's quite a bit more to making a content editable behave like an input but in answer to your question, this works in Chrome 98 browser.
const myEditableContainer = document.querySelector('.my-editable__container');
const button = document.querySelector('button');
let disabled = false;
const buttonText = (disabled) => `Click to ${disabled ? 'enable' : 'disable'}`;
button.innerHTML = buttonText(disabled);
button.addEventListener('click', () => {
disabled = !disabled;
button.innerHTML = buttonText(disabled);
myEditableContainer.classList.toggle('my-editable-container--disabled', disabled);
});
.my-editable__container {
position: relative;
}
.my-editable__cover {
position: absolute;
display: block;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: 0;
pointer-events: none;
background-color: white;
}
.my-editable-container--disabled .my-editable__cover {
pointer-events: all;
opacity: 0.5;
}
<div style="margin:30px">
<div class="my-editable__container">
<span class="my-editable" contenteditable="true" style="background:#eee">
Hello World
</span>
<span class="my-editable__cover"></span>
</div>
<button>
Enable/Disable
</button>
<div>Outside</div>
</div>

How to make DIV constantly display text on click?

I want a way (ideally just using CSS HTML) where when I click on Question, the text shows and remains there. Then if they click on text again, it will revert back to question.
It works on hover, but I don't know how to make it on click. HTML:
<div id="packagequestioninfo">
<p class="packagereplies">Question</p>
<p class="packagecomment">If you require <b>hosting</b> for your website, select your primary website and go to <b>Part II</b>. If you do not require hosting, please Checkout below. </p>
</div>
CSS:
#packagequestioninfo {
padding: 10px;
background: #F2F7FA;
border-radius: 0px;
-moz-box-shadow: 0 0 5px #ccc;
-webkit-box-shadow: 0 0 5px #ccc;
box-shadow: 0 0 5px #ccc;
}
#packagequestioninfo:hover {
background-image:url(../img/index/body/ourproducts/light_blue_background_pattern.jpg);
background-repeat:repeat;
cursor: none;
}
#packagequestioninfo .packagecomment {
display: none;
}
#packagequestioninfo:hover .packagereplies {
display: none;
}
#packagequestioninfo:hover .packagecomment {
display: inline;
}
Here is the code http://jsfiddle.net/53UK2/
Make .packagecomment invinsible with css, then use jQuery:
$('p').click(function(){
$('p').toggle();
});
Fiddle: http://fiddle.jshell.net/RcTGN/
If you do not need IE8 and lower support you can do it with pure CSS like this.
This is because of the ":checked" css selector. See support here.
The HTML:
<div class="question">
<input type="checkbox" class="qestion-checkbox" id="q1" />
<label for="q1" class="qestion-text">Question 1 text</label>
<label for="q1" class="qestion-answer">Question 1 answer</label>
</div>
<div class="question">
<input type="checkbox" class="qestion-checkbox" id="q2" />
<label for="q2" class="qestion-text">Question 2 text</label>
<label for="q2" class="qestion-answer">Question 2 answer</label>
</div>
The CSS:
.qestion-answer, .qestion-checkbox {
display: none;
}
.qestion-checkbox:checked + .qestion-text {
display:none;
}
.qestion-checkbox:checked + .qestion-text + .qestion-answer {
display:block;
}
If you do need IE8 and lower support you need to use some javascript/jQuery
It's impossible using only css. Of course you can change :hover instead :active, but it will be work when the mouse button is held down.
Check it.
Why don't you want use jquery? It will be easier.
Non-jquery way...
Just showing and hiding by changing the display style attribute:
HTML:
<script src="javascriptfile.js"></script>
<div id="packagequestioninfo">
<p class="packagereplies">Question</p>
<p class="packagecomment">If you require <b>hosting</b> for your website, select your primary website and go to <b>Part II</b>. If you do not require hosting, please Checkout below. </p>
</div>
javascriptfile.js:
// Wait for the entire window elements to be loaded
window.onload = function() {
document.addEventListener('packagequestioninfo').addEventListener('onclick', function() {
if(document.getElementById('packagecomment').style.display !== 'none') {
document.getElementById('packagecomment').style.display = 'none';
} else {
document.getElementById('packagecomment').style.display = 'block';
}
});
}
or if you'd like to go with the class toggling method:
window.onload = function() {
document.addEventListener('packagequestioninfo').addEventListener('onclick', function() {
if(document.getElementById('packagecomment').className.indexOf('show')) {
document.getElementById('packagecomment').className = '';
} else {
document.getElementById('packagecomment').className = 'show';
}
});
}
in this approach you'll need to add a class to your CSS:
.show{
display:block;
}

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>
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.
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>
jQueryUI has a modal dialog plugin. It won't release control simply by clicking the background, as you requested: http://jqueryui.com/demos/dialog/#modal
Show Modal Box
<div id="modalContents" style="display:none;">
<textarea>Hello World</textarea>
</div>
--
$(".showModal").click(function(e){
e.preventDefault();
$("#modalContents").dialog({bgiframe: true, height: 140, modal: true});
});
a Modal window by definition requires action to be taken by the user before it can be returned to the main window. So what you are looking for is not a modal but a window.
html5 on chrome has the <dialog> element. Do experiment with it
I think the below code will help you out
<!DOCTYPE html>
<html>
<title>login modal</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-container">
<h2>Login Modal</h2>
<button onclick="document.getElementById('id01').style.display='block'" class="w3-button w3-green w3-large">Login</button>
<div id="id01" class="w3-modal">
<div class="w3-modal-content w3-card-4 w3-animate-zoom" style="max-width:600px">
<div class="w3-center"><br>
<span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-xlarge w3-hover-red w3-display-topright" title="Close Modal">×</span>
</div>
<form class="w3-container" action="/action_page.php">
<div class="w3-section">
<label><b>Username</b></label>
<input class="w3-input w3-border w3-margin-bottom" type="text" placeholder="Enter Username" name="usrname" required>
<label><b>Password</b></label>
<input class="w3-input w3-border" type="password" placeholder="Enter Password" name="psw" required>
<button class="w3-button w3-block w3-green w3-section w3-padding" type="submit">Login</button>
<input class="w3-check w3-margin-top" type="checkbox" checked="checked"> Remember me
</div>
</form>
<div class="w3-container w3-border-top w3-padding-16 w3-light-grey">
<button onclick="document.getElementById('id01').style.display='none'" type="button" class="w3-button w3-red">Cancel</button>
<span class="w3-right w3-padding w3-hide-small">Forgot password?</span>
</div>
</div>
</div>
</div>
</body>
</html>
A modal popup that does not disappear on click:
var popup = document.getElementById("popup");
function openPopup() {
popup.style.display = "block";
}
function closePopup() {
popup.style.display = "none";
}
/* Popup container */
.popup {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgba(0, 0, 0, 0.4);
/* Black background with opacity */
}
/* Popup content */
.popup-content {
background-color: #fefefe;
margin: 15% auto;
/* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%;
/* Could be more or less, depending on screen size */
}
/* Close button */
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
<button onclick="openPopup()">Edit</button>
<div id="popup" class="popup">
<div class="popup-content"><span class="close" onclick="closePopup()">×
</span>
<form><label for="name">Name:</label><input type="text" id="name" name="name" value="shdhd"><br><br><label for="email">Email:</label><input type="email" id="email" name="email"><br><br><input type="submit" value="Save"></form>
</div>
</div>