Opening modal with hash - javascript

I want to open a modal with hash (example: www.example.com/#example.) This is my code below but the modal not showing. Thanks for your answer and I appreciate it.
$(document).ready(function() {
$('.popup').click(openModalPopupClick);
function openModalPopup(){
var hashText = window.location.hash.substr();
if (hashText){
$('#'+hashText).modal('show');
}
}
function openModalPopupClick(){
var hashText = $(this).attr('href');
if (hashText){
$(hashText).modal('show');
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
click
<div class="modal fade" id="regis-new-company" role="dialog" aria-labelledby="myLargeModalLabel">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header"></div>
<div class="modal-body"></div>
<div class="modal-footer"></div>
</div>
</div>
</div>

hashText allready have # so $('#'+hashText).modal('show'); should be $(hashText).modal('show'); and call openModalPopupClick function on ready. i have update your code.
$(document).ready(function() {
$('.popup').click(openModalPopupClick);
function openModalPopup(){
var hashText = window.location.hash.substr();
if (hashText){
$(hashText).modal('show');
}
}
openModalPopup();
function openModalPopupClick(){
var hashText = $(this).attr('href');
if (hashText){
$(hashText).modal('show');
}
}
});

Related

How to bind my current Popup code in javascript to show the popup only once per day?

I am using below code to show a popup on my home page.
But the problem is that it shows every time a user reloads the page. Instead of that I would like to be displayed once per day.
Here is the code:
$(document).ready(function() {
$("#myModal").modal('show');
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<div id="myModal" class="modal fade" style="padding-top:30%;">
<div class="modal-dialog">
My Content of pop up is here.
</div>
</div>
I tried to bind it with cookie expiration but I couldn't do it.
you can do it like this
$(document).ready(function() {
var todayCodeRecord = localStorage.getItem('today')
var todayCode = new Date().toLocaleDateString()
if(todayCodeRecord !== todayCode) {
$("#myModal").modal('show');
localStorage.setItem('today', todayCode)
}
});

Modal close on submit

I have this modal window that pops up when you open a page. I have problems closing it after submitting a form. It closes with a button and when you click outside it, but somehow I can't close it with a submit button.
My scripts
var modal = document.getElementById('myModal');
var span = document.getElementsByClassName("close")[0];
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
The button I want to close the modal with.
<input type="submit" name="submit" value="Send" > </form>
use elem.modal()
$('#myModal').modal('hide')
Bootstrap need a jquery .so better do with jquery function
$(document).ready(function() {
$('#myModal').modal('show')
$('form').on('submit', function(e) {
e.preventDefault();
$('#myModal').modal('hide')
})
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Modal Methods</h4>
</div>
<div class="modal-body">
<form>
<input type="submit" name="submit" value="Send"> </form>
</div>
</div>
</div>
</div>

How to display and shut message modal when hovering over buttons

I would like to display a text description box when I hover over the button and close it when not hovered. At the moment, when my button is hovered the text box displays, however I need to click off the screen to close it. When I try to use several buttons to show different message boxes, all the modals are opened and I need to click the screen many times to close it. How can I show the message box on hover and undisplay when the mouse is not hovered over. How can this be achieve for multiple buttons?
Below is what i have so far:
$(document).ready(function () {
"use strict";
$("#test1").hover(
function () {
$('.modal').modal({
show: true
});
},
function () {
$('.modal').modal('hide');
});
});
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<button class="btn login btn-primary" data-toggle="modal" data-target="#product1" id="test1">Test</button>
<div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
This text box shown when mouse hover over the button
</div>
</div>
</div>
First major problem: when bootstrap displays this modal, it overwrites everything on the web page, including the Test button, which makes it out of reach of a mouseOut (the bottom of the modal dialog is over the button )
This is solved by bidding on the order of the display layers by placing a larger z-index css (I put 8000 and this also works only if the element uses a positioning)
As a result, this is no longer a true modal dialog, and the click on the Test button can be triggered, which is contrary to the spirit of using a modal dialog.
the BS4 doc on the subject => https://getbootstrap.com/docs/4.0/components/modal/#via-data-attributes
$(document).ready(function () {
"use strict";
$(".BtModals").hover(
function () {
let refID = '#' + $(this).data('modal_id');
$(refID).modal('show');
},
function () {
let refID = '#' + $(this).data('modal_id');
$(refID).modal('hide');
});
});
.BtModals:hover {
position:relative;
z-index: 8000;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous">
</script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
</script>
<button class="btn login btn-primary BtModals" data-modal_id="Modal-t1" data-toggle="modal" data-target="#product1" >Test 1</button>
<button class="btn login btn-primary BtModals" data-modal_id="Modal-t2" data-toggle="modal" data-target="#product1" >Test 2</button>
<div id="Modal-t1" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel"
aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
This text box shown when mouse hover over the button Test 1
</div>
</div>
</div>
<div id="Modal-t2" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel"
aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
This text box shown when mouse hover over the button Test 2
</div>
</div>
</div>
New Answer:
"hover" method accepts second argument as handlerOut.
So you can do something like this:
$(document).ready(function () {
"use strict";
$("#test1").hover(
function () {
$('.modal').modal({
show: true
});
},
function () {
$('.modal').modal('hide');
});
});
Old Answer:
Since you use bootstrap, for such cases, you can use popover.
https://getbootstrap.com/docs/3.3/javascript/#popovers-examples
<button type="button" class="btn btn-default" data-toggle="tooltip" data-placement="left" title="Tooltip on left">Tooltip on left</button>
You can achieve it with CSS by setting a data attribute with the 'hover text' in the HTML for each button. If there are too many buttons to do it manually, or if they change dynamically, you can loop over them in JS and use .setAttribute("data-tip", "tip_content").
Here is an example of the HMTL and CSS:
<button data-tip="Show this on hover">My botton</button>
button:hover:after {
content: attr(data-tip);
color: red;
display: block;
position: absolute;
top: 20%;
}
Here is the codepen link if you want to see it live: https://codepen.io/CPC464/pen/KEbJJo

Modal Hide Doesn't Work In Bootstrap 4

I have problem with hiding modal in bootstrap 4.
In my tmp function I have to close modal after that I need use method update_table(url)
HTML and JS
<div class="modal" id="Modal" tabindex="-1" role="dialog"></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script type="text/javascript">
function abrir_modal(url) {
$('#Modal').load(url, function () {
$(this).modal('show');
});
return false;
}
function tmp(url) {
$('#Modal').on('shown.bs.modal', function (e) {
$("#Modal").modal('hide');
})
update_table(url);
}
function update_table(url) {
$.ajax({
type: "GET",
url: url
})
.done(function () {
refresh_table();
});
}
function refresh_table() {
$.ajax({
type: "GET",
url: "{% url 'Project:Task_Schedule_TableView' %}"
})
.done(function (response) {
$("#_appendHere").load("{% url 'Project:Task_Schedule_TableView' %}" + "#_appendHere");
});
};
function hide_modal() {
console.log($('#Modal').modal('name'))
$('#Modal').modal('hide');
console.log(33)
return false;
}
</script>
I don't know what is wrong but when I try use the hide_modal function instead of the tmp function, modal is hidden.
This is simple code for Bootstrap 4 Modal Pop Up which hide the pop up.
You can check this..
$('#Modal').modal('show');
function tmp(url) {
$("#Modal").modal('hide');
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<div class="modal" id="Modal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h4 class="modal-title">Modal Heading</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
Modal body..
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
When the pop up is shown call this function from browser console.. tmp('http://test/test'); it will hide the pop up. It same as Bootstrap 3
Step 1. Remove these lines from your code
$('#Modal').on('shown.bs.modal', function (e) {
$("#Modal").modal('hide');
})
Step 2. put the hide_modal function top of all code.
Then it will be appeared properly.

Adding a jquery timepicker into a modal that was creates by angular

Repository for Project
here is the repository to see what i have done so far. I got the clock and modal to combine nicely however the popup for the modal seems to be layered behind the modal itself. I know that jquery and angular don't play nice together. However i have gotten an older version of this to work with a modal however i need to use the newest version because the callbacks have information that i need. If anyone has suggestions i would really appreciate it! Thankyou!
Here are some code snip its from the repository
index.html inside the folder specified clockpicker-gh-pages:
<html>
<head>
<title>ClockPicker</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="dist/bootstrap-clockpicker.min.css">
<link rel="stylesheet" type="text/css" href="assets/css/github.min.css">
</head>
<body ng-app="mymodal">
<div ng-controller="MainCtrl" class="container">
<button ng-click="toggleModal('Success')" class="btn btn-default">Success</button>
<button ng-click="toggleModal('Remove')" class="btn btn-default">Remove</button>
<button ng-click="toggleModal('Deny')" class="btn btn-default">Deny</button>
<button ng-click="toggleModal('Cancel')" class="btn btn-default">Cancel</button>
<modal visible="showModal">
<div class="input-group clockpicker">
<input type="text" class="form-control" value="09:30">
<span class="input-group-addon">
<span class="glyphicon glyphicon-time"></span>
</span>
</div>
<script type="text/javascript" class="ng-scope">$('.clockpicker').clockpicker();</script>
</modal>
</div>
<script src="../Scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="assets/js/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
<script type="text/javascript" src="dist/bootstrap-clockpicker.min.js"></script>
<script src="../Scripts/angular.js"></script>
<script src="app.js"></script>
</body>
</html>
and from this folder there is a modal app.js script that contains:
var mymodal = angular.module('mymodal', []);
mymodal.controller('MainCtrl', function ($scope) {
$scope.showModal = false;
$scope.buttonClicked = "";
$scope.toggleModal = function (btnClicked) {
$scope.buttonClicked = btnClicked;
$scope.showModal = !$scope.showModal;
};
});
mymodal.directive('modal', function () {
return {
templateUrl: 'userOptionsModal.html',
restrict: 'E',
transclude: true,
replace: true,
scope: true,
link: function postLink(scope, element, attrs) {
scope.$watch(attrs.visible, function (value) {
if (value == true)
$(element).modal('show');
else
$(element).modal('hide');
});
$(element).on('shown.bs.modal', function () {
scope.$apply(function () {
scope.$parent[attrs.visible] = true;
});
});
$(element).on('hidden.bs.modal', function () {
scope.$apply(function () {
scope.$parent[attrs.visible] = false;
});
});
}
};
});
also to format the modal window the html file that is specified as User options modal is the template for the modal window.
<div class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">{{ buttonClicked }} Hello world!</h4>
</div>
<div class="modal-body" ng-transclude>
</div>
</div>
</div>
</div>
please keep in mind most of the code that is listed here is within the folder labeled clockpicker-gh-pages. If this helps let me know!
In angular's directive documentation, it says the element passed into the link function for a directive is a "jqLite-wrapped element".
So in your example, it appears to me that the line of code $(element).on( ... ) should be element.on( ... ).
EDIT: I didn't see the $(element).modal( ... ) lines. Though I've never used modal before I believe that should be modified in the same way.
The other option would be to put an id on your angular directive template, and access it like this.
$('#myelementid').modal( ... ) and $('#myelementid').on( ... )

Categories