Trigger the same action with two IDs - javascript

I have a modal which I would like to call with two buttons with different IDs. The reason is that one button is displayed on desktop and the other on mobile view (responsive). display: none does do the trick of using two times the same ID.
The modal:
<!-- The Modal -->
<div id="myModal" class="modal">
<span class="closecross">×</span>
<!-- Modal content -->
<div class="modal-content">
<p>Content</p>
</div>
</div>
The modal as JS:
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("closecross")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
My two buttons:
<div class="insertClass" id="myBtnMobile">Modal</div>
And
<div class="insertClass" id="myBtn">Modal</div>
I tried to use getElementsByClassName('.myBtnMobile','.myBtn') but it does not work with my JS.
The code I am using is from https://www.w3schools.com/howto/howto_css_modals.asp just to provide some potential playground.
Thanks in advance!

Working fiddle
You could use queryselectorAll() :
var divs = document.querySelectorAll('#myBtnMobile,#myBtn');
divs.forEach(function(div) {
div.addEventListener("click", clickEvent);
});
function clickEvent() {
console.log('Div ' + this.id + ' Clicked');
}
<div id="myBtnMobile">Modal</div>
<div id="myBtn">Modal</div>

You can just use this:
<div class="insertClass" id="myBtnMobile" onclick="yourfunction()">Modal</div>
<div class="insertClass" id="myBtn" onclick="yourfunction()">Modal</div>
or
document.getElementById("myBtnMobile").addEventListener("click", yourfunction);
document.getElementById("myBtn").addEventListener("click", yourfunction);
Or use
<button class="...">
not
<button id="...">.
One more possible solution:
var buttons = document.getElementsByClassName('insertClass');
for (var i = 0; i < buttons.length; i++)
{
buttons[i].onclick = function() {
document.getElementById('myModal').style.display = 'block';
}
}
https://jsfiddle.net/43gkcm92/24/

you may this way to trigger two ID with same action
$('#myBtnMobile,#myBtn').click(function () {
// your logic
});

You need to access the class and not the ids:
document.getElementsByClassName("insertClass");

Related

My modal not popping up after clicking on button

I followed the w3schools tutorial for modals https://www.w3schools.com/howto/howto_css_modals.asp but when i click on the button, nothing happens. I've actually followed two previous modal tutorials and on each one, nothing happens when i click the button. I've checked that the external js file is working.
$(window).on('scroll', function() {
if ($(window).scrollTop()) {
$('nav').addClass('black');
} else {
$('nav').removeClass('black');
}
})
$(document).ready(function() {
$(".menu h4").click(function() {
$("nav ul").toggleClass("active")
})
})
//modal
var modal = document.getElementsByClassName('modal-container');
// Get the button that opens the modal
var btn = document.getElementsByClassName("modal-btn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close");
// When the user clicks the button, open the modal
btn.onclick = function() {
modal[0].style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="modal-btn"> Click me</button>
<div class="modal-container">
<div class="modal-content">
<span class="close">×</span>
<p> Some text in modal</p>
</div>
</div>
getElementsByClassName returns an object (NodeList) that resembles an array, and doesn't return a single element as you were expecting
getElementById returns a single element, and that's what you should be using for a modal
$(window).on('scroll', function() {
if ($(window).scrollTop()) {
$('nav').addClass('black');
} else {
$('nav').removeClass('black');
}
})
$(document).ready(function() {
$(".menu h4").click(function() {
$("nav ul").toggleClass("active")
})
})
//modal
var modal = document.getElementsByClassName('modal-container')[0];
// Get the button that opens the modal
var btn = document.getElementsByClassName("modal-btn")[0];
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="modal-btn"> Click me</button>
<div class="modal-container">
<div class="modal-content">
<span class="close">×</span>
<p> Some text in modal</p>
</div>
</div>

Multiple modal windows on a single page (flexible JS solution)

The Issue
I'm looking to implement a flexible javascript solution to opening and closing multiple modal windows on the same page.
I have found way to open multiple modal windows in such a manner, but I can't for the life of me figure out how to close them by the click of a button or by clicking outside of the modal window itself.
Here is my javascript code:
var openModal = document.getElementsByClassName("open-modal");
for (var i = 0; i < openModal.length; i++) {
var thisOpenModal = openModal[i];
thisOpenModal.addEventListener("click", function() {
var modal = document.getElementById(this.dataset.modal);
modal.style.display = "block";
}, false);
}
(I didn't write this code myself, it's an edited copy of "pgk's" answer over on this page: Opening multiple modal boxes on one page)
I trigger the modal windows by adding the 'open-modal' class to my "buttons" and I use 'data-modal' in relation to the id's of the modal windows:
<div class="featurette-wrap featurette--anchor open-modal" data-modal="modal-microsoft-account">
Opprett konto
</div>
The button above triggers the modal window with the id of modal-microsoft-account:
<div class="modal" id="modal-microsoft-account">
Microsoft-konto
</div>
So my question is:How can I implement a way to close a modal window once it's opened?
I've tried doing this, but I can't get it to work (from W3schools):
// When the user clicks on <span> (x), close the modal
closeModal.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
It's better to use event listeners to track actions on elements as I've done below;
also, making use of the setAttribute function when setting styles with Javascript ensures that every other style assigned to that element is overridden and the style you are setting always applies (except in cases where another style is set to be important)
var openModal = document.getElementsByClassName("open-modal");
for (var i = 0; i < openModal.length; i++) {
var thisOpenModal = openModal[i];
var targetModal = thisOpenModal.getAttribute('data-modal')
//get the id of the modal to open based on thisOpenModal
var thisTargetModal = document.getElementById(targetModal)
//get element
thisOpenModal.addEventListener("click", function(event) {
thisTargetModal.setAttribute('style', "display:block;")
//code to open modal on click of div with id=""
}, false);
// When the user clicks on <span> (x), close the modal
var closeModal = document.querySelector('#' + thisTargetModal.id, "span");
//code to listen to click event and change block style attribute to none on click
closeModal.addEventListener("click", function() {
thisTargetModal.setAttribute('style', "display:none;")
}, false)
// When the user clicks anywhere on the modal div, close it
window.addEventListener("click", function(event) {
//code to listen to click event on window and change block style attribute to none
if (event.target == targetModal) {
thisTargetModal.setAttribute('style', "display:none;")
}
})
}
<div class="featurette-wrap featurette--anchor open-modal" data-modal="modal-microsoft-account">
Opprett konto
</div>
<div class="modal" id="modal-microsoft-account" style="display:none;">
<p>Microsoft-konto </p>
<p>
<span id="close-modal">
x
</span>
</p>
</div>
So I actually ended up finding a solution that did exactly what I was looking for over at W3Schools forum: http://w3schools.invisionzone.com/topic/55976-multiple-modal-boxes-on-webpage/
This did the trick 🙌:
// Get the modal
var modal = document.getElementsByClassName("modal");
// Get the button that opens the modal
var openModal = document.getElementsByClassName("open-modal");
// Get the <span> element that closes the modal
var closeModal = document.getElementsByClassName("modal__close");
// When the user clicks the button, open the modal
function setDataIndex() {
for (i = 0; i < openModal.length; i++) {
openModal[i].setAttribute('data-index', i);
modal[i].setAttribute('data-index', i);
closeModal[i].setAttribute('data-index', i);
}
}
for (i = 0; i < openModal.length; i++) {
openModal[i].onclick = function() {
var ElementIndex = this.getAttribute('data-index');
modal[ElementIndex].style.display = "block";
};
// When the user clicks on <span> (x), close the modal
closeModal[i].onclick = function() {
var ElementIndex = this.getAttribute('data-index');
modal[ElementIndex].style.display = "none";
};
}
window.onload = function() {
setDataIndex();
};
window.onclick = function(event) {
if (event.target === modal[event.target.getAttribute('data-index')]) {
modal[event.target.getAttribute('data-index')].style.display = "none";
}
};

Creating a function in Javascript and using it with a button

I am trying to get this button to run the function. I know I can get rid of 'function showWeb' in the Javascript and the code will be running fine, I am trying to get it to work so that I can use it as a function so I can make an instance of the object.
<button id='myBtn' onclick='showWeb()'>Open Modal</button>
<div id='myModal' class='modal'>
<div class='modal-content'>
<span class='close'>×</span>
<p>$link</p>
</div>
</div>
<script>
function showWeb(){
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
}
</script>
I'm not sure what you are trying to get at, but please do consider that a function must only do a single task, it would be great if you refactored your code this way.
function openModal() {
var modal = document.getElementById('myModal');
modal.style.display = "block";
}
function closeModal() {
var modal = document.getElementById('myModal');
modal.style.display = "none";
}
window.onclick = function(event) {
var modal = document.getElementById('myModal');
if (event.target == modal) {
modal.style.display = "none";
}
}
<button id='myBtn' onclick='openModal()'>Open Modal</button>
<div id='myModal' class='modal'>
<div class='modal-content'>
<span class='close' onclick='closeModal()'>×</span>
<p>$link</p>
</div>
</div>
First of all, you wouldn't want to retrieve the button inside the function.
Second of all, those onclick events are not being attached right (typo in this case)
what you want to do is:
var modal = document.getElementById('myModal');
var btn = document.getElementById('myBtn');
var span = document.getElementsByClassName('close')[0];
btn.addEventListener('click', showModal());
span.addEventListener('click', hideModal());
function showModal() {
modal.style.display = 'block';
};
function hideModal() {
modal.style.display = 'none';
};
window.addEventListener('click', function(event) {
if (event.target != modal) { // needs to be anything but the modal, from what i can understand
hideModal();
}
});
#myModal {
display: none;
background: #dd4535;
}
<button id='myBtn'>Open Modal</button>
<div id='myModal' class='modal'>
<div class='modal-content'>
<span class='close'>×</span>
<p>$link</p>
</div>
</div>
No jquery this time, since it was obviously not well accepted. Just plain JS

How do I create a second close button on my modal (in addition to the top left x)

I'd like to add another "Cancel" button on the bottom of the modal in addition to the span option to close the modal. Not sure where to go from here please advise on some options. Thanks
<!-- The Offer Modal-->
<!-- Trigger/Open The Modal -->
<button id="makeOfferBtn">Make an Offer</button>
<!-- The Offer Modal content -->
<div id="myOfferModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById('myOfferModal');
// Get the button that opens the modal
var btn = document.getElementById("makeOfferBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
This is a quick and dirty example that will work with any number of buttons that has the .close class defined:
// Selecting all nodes that are `.close`
var closeBtns = document.querySelectorAll('.close');
// Iterating over on all of them and
// attaching handlers for the 'click' event.
for (var i = 0, max = closeBtns.length; i < max; i++) {
var close = closeBtns[i];
close.addEventListener('click', function() {
modal.style.display = 'none';
});
}
However, I would recommend to restructure your code a bit in order to not to use so many global variables.

Pop Up modal only works on first item

I am working on a shopify store, there is a 'remove from cart' option on the cart page, when they click on it a popup shows up to confirm if they want to remove it from the cart.
The issue I am having is that the popup will only work on the first item, the others will just take you back to the top of the page.
Here's the code I am using.
The remove text
<td style="text-align: center">
X
</td>
The Popup
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<p class="modal-index-title">Hey!</p>
<p class="modal-title">Are you sure you want to remove this?</p>
<div class="modal-confirm">
<span class="close">No!</span>
<p>Yes</p>
</div>
</div>
</div>
The script
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("cart_popup");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
IDs are supposed to be unique for a DOM element so your JS code will just grab the first instance of the cart_popup element and apply the onClick there.
If you want to make it apply across all your remove cart buttons, add a CSSclass to it and then apply the btn.onclick function to buttons having that class.
EDIT:
Example:
var classname = document.getElementsByClassName("classname");
var showModal = function() {
modal.style.display = "block";
};
for (var i = 0; i < classname.length; i++) {
classname[i].addEventListener('click', showModal, false);
}
You might have several elements with the same id attribute. Only the first triggers your btn.onclick function.

Categories