How to open bootstrap modal using a button clickHow do JavaScript closures work?How to horizontally center a <div>?How do I detect a click outside an element?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I redirect to another webpage?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
Report how much space is used and available in storage in ZFS on FreeBSD
To accent or not to accent in Greek
Find values of x so that the matrix is invertible
Can I call 112 to check a police officer's identity in the Czech Republic?
Why is dry soil hydrophobic? Bad gardener paradox
Won 50K! Now what should I do with it
How can an advanced civilization forget how to manufacture its technology?
Data standardization vs. normalization for clustering analysis
How does one stock fund's charge of 1% more in operating expenses than another fund lower expected returns by 10%?
If the derivative of a function is square of it then it is constant
Is `curl something | sudo bash -` a reasonably safe installation method?
Are lithium batteries allowed in the International Space Station?
Why do they not say "The Baby"
Why the term 'unified' in "unified mass unit"?
What is the English equivalent of 干物女 (dried fish woman)?
Is it rude to tell recruiters I would only change jobs for a better salary?
Hacker Rank : Electronics Shop
What does "Fotze" really mean?
In which ways do anagamis still experience ignorance?
Modeling, view and projection transformation using vector and point in homogenous form
Should you avoid redundant information after dialogue?
Can a continent naturally split into two distant parts within a week?
Bob's unnecessary trip to the shops
Measuring mystery distances
How to open bootstrap modal using a button click
How do JavaScript closures work?How to horizontally center a <div>?How do I detect a click outside an element?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?How do I redirect to another webpage?How can I know which radio button is selected via jQuery?How to check whether a checkbox is checked in jQuery?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have developed a simple js pig game. You can see it here: http://creativeartbd.com/demo/game/js-pig-game
At first, it will open a Bootstrap Modal box to set a winning score. Now, the game will begin by click on the Roll Dice button.
Well, now if you WIN then you can press the New Game button to re-play the game. Here, I want to open the bootstrap modal again when I click on the New Game button. For that I have write this code:
document.querySelector(".btn-new-game").addEventListener("click", function ()
init();
document.getElementById("myModal").modal('show');
);
but it's showing me an error message on console log:
Uncaught TypeError: document.getElementById(...).modal is not a function
Maybe this is because of Javascript IIFE. I don't know exactly :(
I have also tried with this code:
document.getElementById("myModal").click();
but no luck :(
My HTML and js code is a bit long that's why I don't put here. You can find it here: https://jsfiddle.net/r8cga7L5/
Thanks.
javascript html
|
show 3 more comments
I have developed a simple js pig game. You can see it here: http://creativeartbd.com/demo/game/js-pig-game
At first, it will open a Bootstrap Modal box to set a winning score. Now, the game will begin by click on the Roll Dice button.
Well, now if you WIN then you can press the New Game button to re-play the game. Here, I want to open the bootstrap modal again when I click on the New Game button. For that I have write this code:
document.querySelector(".btn-new-game").addEventListener("click", function ()
init();
document.getElementById("myModal").modal('show');
);
but it's showing me an error message on console log:
Uncaught TypeError: document.getElementById(...).modal is not a function
Maybe this is because of Javascript IIFE. I don't know exactly :(
I have also tried with this code:
document.getElementById("myModal").click();
but no luck :(
My HTML and js code is a bit long that's why I don't put here. You can find it here: https://jsfiddle.net/r8cga7L5/
Thanks.
javascript html
$("#myModal").modal('show');
– Pranav C Balan
Mar 26 at 5:55
you can use$("#myModal").modal('open');
$("#myModal").modal('show');
$("#myModal").modal('toggle');
– Devsi Odedra
Mar 26 at 5:56
then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function
– creativeartbd
Mar 26 at 5:58
You already using model on window load using Jquery the same way you can open model on button click also.document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );
– Sethuraman
Mar 26 at 6:07
1
please check this fiddle with what i have added the same code of yours like$('#myModal').modal('show');
and its working -- jsfiddle.net/z12uL3ba
– Sethuraman
Mar 26 at 6:32
|
show 3 more comments
I have developed a simple js pig game. You can see it here: http://creativeartbd.com/demo/game/js-pig-game
At first, it will open a Bootstrap Modal box to set a winning score. Now, the game will begin by click on the Roll Dice button.
Well, now if you WIN then you can press the New Game button to re-play the game. Here, I want to open the bootstrap modal again when I click on the New Game button. For that I have write this code:
document.querySelector(".btn-new-game").addEventListener("click", function ()
init();
document.getElementById("myModal").modal('show');
);
but it's showing me an error message on console log:
Uncaught TypeError: document.getElementById(...).modal is not a function
Maybe this is because of Javascript IIFE. I don't know exactly :(
I have also tried with this code:
document.getElementById("myModal").click();
but no luck :(
My HTML and js code is a bit long that's why I don't put here. You can find it here: https://jsfiddle.net/r8cga7L5/
Thanks.
javascript html
I have developed a simple js pig game. You can see it here: http://creativeartbd.com/demo/game/js-pig-game
At first, it will open a Bootstrap Modal box to set a winning score. Now, the game will begin by click on the Roll Dice button.
Well, now if you WIN then you can press the New Game button to re-play the game. Here, I want to open the bootstrap modal again when I click on the New Game button. For that I have write this code:
document.querySelector(".btn-new-game").addEventListener("click", function ()
init();
document.getElementById("myModal").modal('show');
);
but it's showing me an error message on console log:
Uncaught TypeError: document.getElementById(...).modal is not a function
Maybe this is because of Javascript IIFE. I don't know exactly :(
I have also tried with this code:
document.getElementById("myModal").click();
but no luck :(
My HTML and js code is a bit long that's why I don't put here. You can find it here: https://jsfiddle.net/r8cga7L5/
Thanks.
javascript html
javascript html
asked Mar 26 at 5:54
creativeartbdcreativeartbd
5724 silver badges14 bronze badges
5724 silver badges14 bronze badges
$("#myModal").modal('show');
– Pranav C Balan
Mar 26 at 5:55
you can use$("#myModal").modal('open');
$("#myModal").modal('show');
$("#myModal").modal('toggle');
– Devsi Odedra
Mar 26 at 5:56
then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function
– creativeartbd
Mar 26 at 5:58
You already using model on window load using Jquery the same way you can open model on button click also.document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );
– Sethuraman
Mar 26 at 6:07
1
please check this fiddle with what i have added the same code of yours like$('#myModal').modal('show');
and its working -- jsfiddle.net/z12uL3ba
– Sethuraman
Mar 26 at 6:32
|
show 3 more comments
$("#myModal").modal('show');
– Pranav C Balan
Mar 26 at 5:55
you can use$("#myModal").modal('open');
$("#myModal").modal('show');
$("#myModal").modal('toggle');
– Devsi Odedra
Mar 26 at 5:56
then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function
– creativeartbd
Mar 26 at 5:58
You already using model on window load using Jquery the same way you can open model on button click also.document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );
– Sethuraman
Mar 26 at 6:07
1
please check this fiddle with what i have added the same code of yours like$('#myModal').modal('show');
and its working -- jsfiddle.net/z12uL3ba
– Sethuraman
Mar 26 at 6:32
$("#myModal").modal('show');
– Pranav C Balan
Mar 26 at 5:55
$("#myModal").modal('show');
– Pranav C Balan
Mar 26 at 5:55
you can use
$("#myModal").modal('open');
$("#myModal").modal('show');
$("#myModal").modal('toggle');
– Devsi Odedra
Mar 26 at 5:56
you can use
$("#myModal").modal('open');
$("#myModal").modal('show');
$("#myModal").modal('toggle');
– Devsi Odedra
Mar 26 at 5:56
then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function
– creativeartbd
Mar 26 at 5:58
then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function
– creativeartbd
Mar 26 at 5:58
You already using model on window load using Jquery the same way you can open model on button click also.
document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );
– Sethuraman
Mar 26 at 6:07
You already using model on window load using Jquery the same way you can open model on button click also.
document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );
– Sethuraman
Mar 26 at 6:07
1
1
please check this fiddle with what i have added the same code of yours like
$('#myModal').modal('show');
and its working -- jsfiddle.net/z12uL3ba– Sethuraman
Mar 26 at 6:32
please check this fiddle with what i have added the same code of yours like
$('#myModal').modal('show');
and its working -- jsfiddle.net/z12uL3ba– Sethuraman
Mar 26 at 6:32
|
show 3 more comments
3 Answers
3
active
oldest
votes
Any specific reason you want the modal to open from JS?
Try this for the Bootstrap 4 approach:
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal" 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">
...
</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>
add a comment |
Modify the button like this,
It's working perfectly fine
<button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>
add a comment |
You can just call it like this:
document.getElementById("myModal").modal();
Then it's showing me: TypeError: document.getElementById(...).modal is not a function
– creativeartbd
Mar 26 at 5:59
can you check my js code plz?
– creativeartbd
Mar 26 at 5:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55350639%2fhow-to-open-bootstrap-modal-using-a-button-click%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Any specific reason you want the modal to open from JS?
Try this for the Bootstrap 4 approach:
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal" 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">
...
</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>
add a comment |
Any specific reason you want the modal to open from JS?
Try this for the Bootstrap 4 approach:
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal" 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">
...
</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>
add a comment |
Any specific reason you want the modal to open from JS?
Try this for the Bootstrap 4 approach:
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal" 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">
...
</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>
Any specific reason you want the modal to open from JS?
Try this for the Bootstrap 4 approach:
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal" 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">
...
</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>
answered Mar 26 at 7:09
Varun JoshiVarun Joshi
10310 bronze badges
10310 bronze badges
add a comment |
add a comment |
Modify the button like this,
It's working perfectly fine
<button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>
add a comment |
Modify the button like this,
It's working perfectly fine
<button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>
add a comment |
Modify the button like this,
It's working perfectly fine
<button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>
Modify the button like this,
It's working perfectly fine
<button data-toggle="modal" data-target="#myModal" class="btn btn-new-game">New Game(+)</button>
answered Mar 26 at 8:32
KarthikKarthik
11 silver badge2 bronze badges
11 silver badge2 bronze badges
add a comment |
add a comment |
You can just call it like this:
document.getElementById("myModal").modal();
Then it's showing me: TypeError: document.getElementById(...).modal is not a function
– creativeartbd
Mar 26 at 5:59
can you check my js code plz?
– creativeartbd
Mar 26 at 5:59
add a comment |
You can just call it like this:
document.getElementById("myModal").modal();
Then it's showing me: TypeError: document.getElementById(...).modal is not a function
– creativeartbd
Mar 26 at 5:59
can you check my js code plz?
– creativeartbd
Mar 26 at 5:59
add a comment |
You can just call it like this:
document.getElementById("myModal").modal();
You can just call it like this:
document.getElementById("myModal").modal();
answered Mar 26 at 5:58
Jan LoisJan Lois
1899 bronze badges
1899 bronze badges
Then it's showing me: TypeError: document.getElementById(...).modal is not a function
– creativeartbd
Mar 26 at 5:59
can you check my js code plz?
– creativeartbd
Mar 26 at 5:59
add a comment |
Then it's showing me: TypeError: document.getElementById(...).modal is not a function
– creativeartbd
Mar 26 at 5:59
can you check my js code plz?
– creativeartbd
Mar 26 at 5:59
Then it's showing me: TypeError: document.getElementById(...).modal is not a function
– creativeartbd
Mar 26 at 5:59
Then it's showing me: TypeError: document.getElementById(...).modal is not a function
– creativeartbd
Mar 26 at 5:59
can you check my js code plz?
– creativeartbd
Mar 26 at 5:59
can you check my js code plz?
– creativeartbd
Mar 26 at 5:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55350639%2fhow-to-open-bootstrap-modal-using-a-button-click%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$("#myModal").modal('show');
– Pranav C Balan
Mar 26 at 5:55
you can use
$("#myModal").modal('open');
$("#myModal").modal('show');
$("#myModal").modal('toggle');
– Devsi Odedra
Mar 26 at 5:56
then its showing this error: bootstrap.min.js:6 Uncaught TypeError: f[b] is not a function
– creativeartbd
Mar 26 at 5:58
You already using model on window load using Jquery the same way you can open model on button click also.
document.querySelector(".btn-new-game").addEventListener("click", function () init(); $('#myModal').modal('show'); );
– Sethuraman
Mar 26 at 6:07
1
please check this fiddle with what i have added the same code of yours like
$('#myModal').modal('show');
and its working -- jsfiddle.net/z12uL3ba– Sethuraman
Mar 26 at 6:32