bootstrap - how to default to show modal instead of using button - javascript

From Bootstrap website, it always use button to show the modal.
I am using React, so I want to use javascript manipulate whether the modal shows.
Is there any way to make the modal show as default?
Modal.js
...
return(
<div class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
)
import "./styles.css";
import Modal from './Model'
export default function App() {
return (
<div className="App">
<h1>123</h1>
<Modal/>
</div>
);
}

you can use jquery to show or hide
$("#id of your modal").modal("show");
$("#id of your modal").modal("hide");
jQuery must be referenced first . before the bootstrap
if you wanna use pure javascript only you can change it manually like bellow:
<!-- Modal -->
<div id="mymodal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" id="close-btn" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button id="close-btn" type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
<script>
// select close buttons
const mymodal = document.getElementById('mymodal');
const close_btn = document.querySelectorAll("#close-btn");
//show the modal
mymodal.style.display = "block"
//close modal function
function hide() {
mymodal.style.display = "none"
}
//clise the modal when you click the close btns
close_btn.forEach(close => {
close.addEventListener("click", hide)
})
</script>
and change the script in order you want

Related

Django: How to open a small window on button click without changing url

I am using Django and I want to open a small forms window when I click on button 'Nouvelle Chirurgie' in template without changing the url and I don't know if I have to do it with js or no and how to do it, and after that how to save it in views. Actually, I'm this button take me to another page:
Nouvelle Chirurgie
So, what should I do?
You can use bootstrap for this
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{ form }}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

How show bootstrap modal on window on load in javascript?

// I found it in jquery but i want it in javascript is there any alternative code. Please help, thanks
<script type="text/javascript">
$(window).on('load',function(){
$('#myModal').modal('show');
});
</script>
I believe bootstrap toggles a class on the modal (haven't looked it up recently.
The plain JS equivalent would be
window.addEventListener('load', () => {
const modal = document.querySelector('#myModal');
if (modal) {
modal.classList.add('show');
modal.style.display = 'block';
}
});
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="modal fade" id="myModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
You may need to check the bootstrap docs to be sure what the class name it adds in.

Foundation reveal modal stop executing javascript like alert();

I am writing a program with foundation reveal modal. When a modal is opened, the javascript need to stop executing until user has press the button in the modal, then only the javascript continue execute. This is more similar like Alert() function in javascript. Is it possible?
I need some guidelines, thanks!
Yes this is possible.
HTML:
<div id="modal_div" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Modal body text goes here.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button id="okbtn" type="button" class="btn btn-primary">Ok</button>
</div>
</div>
</div>
</div>
<!-- end html-->
Javascript:
function hide_modal_on_click(top_level_modal_div, button) {
var top_level_modal_div = $("#modal_div").show();
var button = $("#okbtn").on('click', function() {
top_level_modal_div.hide();
})
}
hide_modal_on_click();

Vue.js open modal by click of a button

How to use a button to show the modal in other components? For example, I have the following components:
info.vue
<template>
<div class="container">
<button class="btn btn-info" #click="showModal">show modal</button>
<example-modal></example-modal>
</div>
</template>
<script>
import exampleModal from './exampleModal.vue'
export default {
methods: {
showModal () {
// how to show the modal
}
},
components:{
"example-modal":exampleModal
}
}
</script>
exampleModal.vue
<template>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
hihi
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</template>
How to show the modal from exampleModal.vue? I know that I can
use data-toggle and data-target to show the modal, like this:
<button class="btn btn-info" data-toggle="modal" data-target="#exampleModal">show modal</button>
But is there any way to show the modal by using the method "showModal"?
According to your snippet, you're using a classic Bootstrap modal. You just need to use data-toggle and data-target attributes in that case:
<div id="app">
<div class="container">
<button class="btn btn-info" data-toggle="modal" data-target="#exampleModal">show modal</button>
<example-modal></example-modal>
</div>
</div>
Edit
I misread the question so i edit my answer.
It's possible to open the modal with a custom method. You just need to find the element "#exampleModal" by using refs and then open the modal with a bootstrap method (Bootstrap Programmatic API)
<div id="app">
<div class="container">
<button class="btn btn-info" #click="showModal">show modal</button>
<example-modal ref="modal"></example-modal>
</div>
</div>
methods: {
showModal() {
let element = this.$refs.modal.$el
$(element).modal('show')
}
}
Fiddle example
Without jQuery you could create a function on "method:" block like this:
openEditModal(data){
// <handle with your data>
this.$root.$emit("bv::show::modal", "your-modal-id");
}

Bootstrap modal is not taking the button backward

My code is working but the button is not going backward after clicking.
Here is my output
Launch Demo Modal
Here is the Html code:
<div id="myModal" 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">Confirmation</h4>
</div>
<div class="modal-body">
<p>Do you want to save changes you made to document before closing?</p>
<p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
Here is the javascript:
<script type="text/javascript">
$('#c').click(function(){
$("#myModal").modal('show');
});
i have created a working fiddle.
JS fiddle
Just add a on click event and hide the modal.
HTML
Launch Demo Modal
<div id="myModal" 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">Confirmation</h4>
</div>
<div class="modal-body">
<p>Do you want to save changes you made to document before closing?</p>
<p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button id = "test" type="button" class="btn btn-primary" >Save changes</button>
</div>
</div>
</div>
JS
$('#c').click(function(){
$("#myModal").modal('show');
});
$("#test").on("click", () =>{
alert("save changes")
$("#myModal").modal('hide');
})

Categories

Resources