Trigger click on closest div with angularJS - html

Hi I've got follow code:
angular.module("myApp", []).controller("myController", function($scope) {
$scope.clickedInput = function() {
setTimeout(function() {
angular.element('.addon').triggerHandler('click');
}, 100);
}
$scope.clickedAddon = function(number) {
console.log(number);
}
});
.wrapper {
display: flex;
flex-direction: column;
}
.inputWithAddon {
display: flex;
margin-bottom: 10px;
}
input {
height: 20px;
}
.addon {
width: 26px;
height: 26px;
background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="wrapper" ng-app="myApp" ng-controller="myController">
<div class="inputWithAddon">
<input placeholder="1" class="myInput" ng-click="clickedInput()">
<div class="addon" ng-click="clickedAddon(1)">1</div>
</div>
<div class="inputWithAddon">
<input placeholder="2" class="myInput" ng-click="clickedInput()">
<div class="addon" ng-click="clickedAddon(2)">2</div>
</div>
<div class="inputWithAddon">
<input placeholder="3" class="myInput" ng-click="clickedInput()">
<div class="addon" ng-click="clickedAddon(3)">3</div>
</div>
</div>
My idea is, when I click in an input, it forces a click with triggerHandler() to the green div on the right side and prints his number. It should just force the click of the green div, which is on the right side of the clicked input, not all of them. I wrote today a similar question for JQuery: Force click with trigger() on closest div
There it works fine with more possible solutions. How can I do the same effect with angularjs?
Thanks.

Pass $event to the main ng-click function and then get .parent() and then the 2nd child. Then trigger.
var a = angular
.element($event.target)
.parent().children()
angular
.element(a[1]).triggerHandler('click');
angular.module("myApp", []).controller("myController", function($scope) {
$scope.clickedInput = function($event) {
setTimeout(function() {
var a = angular
.element($event.target)
.parent().children()
angular
.element(a[1]).triggerHandler('click');
}, 100);
}
$scope.clickedAddon = function(number) {
console.log(number);
}
});
.wrapper {
display: flex;
flex-direction: column;
}
.inputWithAddon {
display: flex;
margin-bottom: 10px;
}
input {
height: 20px;
}
.addon {
width: 26px;
height: 26px;
background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="wrapper" ng-app="myApp" ng-controller="myController">
<div class="inputWithAddon">
<input placeholder="1" class="myInput" ng-click="clickedInput($event)">
<div class="addon" ng-click="clickedAddon(1)">1</div>
</div>
<div class="inputWithAddon">
<input placeholder="2" class="myInput" ng-click="clickedInput($event)">
<div class="addon" ng-click="clickedAddon(2)">2</div>
</div>
<div class="inputWithAddon">
<input placeholder="3" class="myInput" ng-click="clickedInput($event)">
<div class="addon" ng-click="clickedAddon(3)">3</div>
</div>
</div>

Related

is there a way to make the ":active" selector's style apply to the currently hovered element when the mouse is moving between elements

when you click on one of the 'cButton' elements, the active style will be applied to it but if you hold the mouse button down and then hover over another cButton while still holding, the style will not be applied to it.
I know a way to do it in Javascript but i am trying to do it using pure css.
body{
display: flex;
justify-content: space-evenly;
}
.cButton{
width: 100px;
aspect-ratio: 1;
background: red;
}
.cButton:active{
background: blue;
}
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
I am afraid that that is impossible in pure css. Because you will need a mouse down hover which is not available in css to my knowledge.
Unfortunately, you can't do this in pure CSS. However, here is some JavaScript code that works:
window.onload = function() {
document.querySelectorAll(".cButton").forEach(function(ele) {
ele.onmousedown = function() {
ele.classList.add("down")
}
ele.onmouseover = function(e) {
if (e.buttons == 1 && e.button == 0) {
ele.classList.add("down");
}
}
ele.onmouseup = function() {
ele.classList.remove("down")
}
ele.onmouseout = function() {
ele.classList.remove("down")
}
});
}
body {
display: flex;
justify-content: space-evenly;
}
.cButton {
width: 100px;
aspect-ratio: 1;
background: red;
}
.cButton.down {
background: blue;
}
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>
<div class="cButton">
</div>

Focus an input field after appending with jQuery

I want to append an input field on click, and focus it so that it has a blinking cursor and can be typed directly. With the following code I am able to append it but i doesn't focus. I first need to click on the field and then I can type.
How can I append and focus?
I have tried following line but it doesn't do anything
$('.fields').append(field_html).focus();
HTML:
<div class="fields">
<!-- append here -->
</div>
<div class="row">
<input name="input" class="input">
</div>
<button>Add</button>
JS:
$('button').on('click', function(e){
var field_html = $('.row').html();
$('.fields').append(field_html);
});
CSS:
.input{
padding: 10px 10px;
border: 1px solid blue;
width: 300px;
margin-bottom: 10px;
}
.row {
display: none;
}
$('button').on('click', function(e){
var field_html = $('.row').html();
$('.fields').append(field_html);
});
.input{
padding: 10px 10px;
border: 1px solid blue;
width: 300px;
margin-bottom: 10px;
}
.row {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="fields">
<!-- append here -->
</div>
<div class="row">
<input name="input" class="input">
</div>
<button>Add</button>
JSFiddle:
https://jsfiddle.net/sLob7cwh/
$('button').on('click', function(e){
var field_html = $('.row').html();
$('.fields').append(field_html).find('input:last').focus();
});
.input{
padding: 10px 10px;
border: 1px solid blue;
width: 300px;
margin-bottom: 10px;
}
.row {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="fields">
<!-- append here -->
</div>
<div class="row">
<input name="input" class="input">
</div>
<button>Add</button>
this code works and will add focus to the latest added input
$('button').on('click', function(e){
var field_html = $('.row').html();
$('.fields').append(field_html).find('input:last').focus();
});
fiddle
JQuery methods generally chain the collections, in the case of .append it passes through the original collection.
Instead, you can use .appendTo which passes through the new html (as it's the original collection)
Change
var container = $("container").append("new_html");
to
var newElements = $("new_html").appendTo("container");
You can then use that result going forward, so your code becomes:
$('button').on('click', function(e){
var field_html = $('.row').html();
$(field_html).appendTo(".fields").find("input.input").focus();
});

How do you display a particular element on hover?

I'm trying to make a navigation bar that displays a particular element when hovered and clicked. I'd also like the other elements to remain hidden. I tried doing a few codes but it won't work even if I hovered it already.
.yearly {
display: none;
}
.evento {
display: none;
}
.year a:hover+.yearly {
display: inline-block;
background-color: #aacde2;
}
.event a:hover+.evento {
display: inline-block;
background-color: #aacde2;
font-size: 50px;
}
<div class="listo">
<ul>
<li class="year">Yearly Donations</li>
<li class="event">Events</li>
</ul>
</div>
<div class="content">
<div class="yearly">
<img src="../images/pic1.jpg" alt="Photo" width="300px">
</div>
<div class="evento">
<p>trial</p>
</div>
</div>
You can achieve this using Jquery.
$(".year a").on({
mouseover: function(e){
$(".yearly").show();
},
mouseout: function(e) {
$(".yearly").hide();
}
});
$(".event a").on({
mouseover: function(e){
$(".evento").show();
},
mouseout: function(e) {
$(".evento").hide();
}
});
.yearly {
display: none;
}
.evento {
display: none;
}
.year a:hover + .yearly {
display: inline-block;
background-color: #aacde2;
}
.event a:hover + .evento {
display: inline-block;
background-color: #aacde2;
font-size: 50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="listo">
<ul>
<li class="year">Yearly Donations</li>
<li class="event">Events</li>
</ul></div>
<div class="content">
<div class="yearly">
<img src="../images/pic1.jpg" alt="Photo" width="300px">
</div>
<div class="evento">
<p>trial</p>
</div>
</div>
You could call a Javascript function on hover. I'm sure there are better ways, but this works for example:
function displayYearly() {
document.getElementById("yearly").style.display = "inline-block";
}
function hideYearly() {
document.getElementById("yearly").style.display = "none";;
}
function displayEvento() {
document.getElementById("evento").style.display = "inline-block";
}
function hideEvento() {
document.getElementById("evento").style.display = "none";
}
#yearly {
display: none;
}
#evento {
display: none;
}
<div class="listo">
<ul>
<li class="year" onmouseover="displayYearly()" onmouseout="hideYearly()">
Yearly Donations
</li>
<li class="event" onmouseover="displayEvento()" onmouseout="hideEvento()">
Events
</li>
</ul>
</div>
<div class="content">
<div id="yearly">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon#2.png" alt="Photo" width="100px">
</div>
<div id="evento">
<p>trial</p>
</div>
</div>

How best to make a smileys box in html

I'd like to add a box containing smileys icons above the comment area which opens using jQuery on click. What I come up with is this:
<div class="emo">
<i href="#" id="showhide_emobox"> </i>
<div id="emobox">
<input class="emoticon" id="icon-smile" type="button" value=":)" />
<input class="emoticon" id="icon-sad" type="button" value=":(" />
<input class="emoticon" id="icon-widesmile" type="button" value=":D" /> <br>
</div>
</div>
css:
.emoticon-smile{
background: url('../smileys/smile.png');
}
#icon-smile {
border: none;
background: url('../images/smile.gif') no-repeat;
}
jQuery:
// =======show hide emoticon div============
$('#showhide_emobox').click(function(){
$('#emobox').toggle();
$(this).toggleClass('active');
});
// ============add emoticons============
$('.emoticon').click(function() {
var textarea_val = jQuery.trim($('.user-comment').val());
var emotion_val = $(this).attr('value');
if (textarea_val =='') {
var sp = '';
} else {
var sp = ' ';
}
$('.user-comment').focus().val(textarea_val + sp + emotion_val + sp);
});
However I have difficulty placing buttons in a nice array and make background image for them (the button values appear before image and the array is not perfectly rectangular. So I'm wondering maybe this is not the best way to render this box.
Any ideas to do this properly?
First show images, on hover hide image and show text. No need for input elements to get text of Dom Node
Something like this:
$(document).ready(function() {
$(".wrapper").click(function() {
var value = $(this).find(".smily-text").text();
console.log(value);
alert("Smily text is '" + value + "'");
});
});
.smily {
background: url(http://www.smiley-lol.com/smiley/manger/grignoter/vil-chewingum.gif) no-repeat center center;
width: 45px;
height: 45px;
}
.smily-text {
display: none;
font-size: 20px;
text-align: center;
line-height: 45px;
height: 45px;
width: 45px;
}
.wrapper {
border: 1px solid red;
float: left;
cursor: pointer;
}
.wrapper:hover .smily {
display: none;
}
.wrapper:hover .smily-text {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<div class="smily"></div>
<div class="smily-text">:)</div>
</div>
<div class="wrapper">
<div class="smily"></div>
<div class="smily-text">:(</div>
</div>
<div class="wrapper">
<div class="smily"></div>
<div class="smily-text">:]</div>
</div>
<div class="wrapper">
<div class="smily"></div>
<div class="smily-text">:[</div>
</div>

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>