I'm trying to open a modal window from navigation bar. I've followed some good tutorials and once I pressed my link a modal opened, problem is model window looks like not editable for some reason, can't even close the X in the modal window. The same works great outside the nav bar.
I would like to know why this happens.
My code is as follows:
<ul class="nav navbar-nav pull-right">
<li class="active"><a href="#">Home</a></li>
<li><a href="#" data-toggle="modal" data-target="#basicModal">Contact Us</a></li>
</ul>
<div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<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" id="myModalLabel">Basic Modal</h4>
</div>
<div class="modal-body">
<h3>Modal Body</h3>
</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>
</div>
Screenshot:
To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.
You can use the Bootstrap . modal('show') method for launching the modal window automatically when page load without clicking anything. A common example of this technique is loading the modal when user landed on the home page and requesting them to subscribe the website newsletter.
User can open modal in same tab by clicking left mouse button on item. User can also click middle mouse button (scrollbar) to open modal in new tab.
Add The State and MethodsAdd a button inside the render block to trigger the modal. When the button is clicked, the isOpen state will be set to true. Now, to display the modal, all you need to do is pass the isOpen state value to the show prop of the <Modal /> component.
<nav class="navbar bg-secondary">
<div
class="collapse navbar-collapse float-right" id="navcol-1" style="color:rgb(255,255,255);">
<ul class="nav navbar-nav ml-auto" role="navigation">
<li><a href="#myModal" data-toggle = "modal" data-target= "#myModal" class="nav-link">Contact us</a></li>
</ul>
</div>
</div>
</nav>
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">For your Queries</h5>
</div>
<div class="modal-body">
IF you have any questions, Mess Manager Office number is <strong>+01234567890</strong> or you can email us by <strong>[email protected]</strong>>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss = "modal">Close</button>
</div>
</div>
</div>
</div>
This is covered in the official documentation:
Modal markup placement
Always try to place a modal's HTML code in a top-level position in your document to avoid other components affecting the modal's appearance and/or functionality.
Put the <div>
that has the modal
class outside of the navbar. You can leave the modal-triggering <a>
in the navbar.
I am experiencing this same issue. The only way I could fix it was by adding a jQuery click on the link in the navbar to override the bootstrap default. Like so:
$("#navbar_register_btn").on("click",function(e){
e.preventDefault();
$('#basicModal').modal('show');
})
You will need to give your link in the navbar an id of "navbar_register_btn" in this case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With