Include html code in modal from bootstrap - html

For school, I have to make my own website that shows some basic things to first years. So I got the idea, that I make a modal with Bootstrap. If you press the card, the modal will pop up.
Now, I want in the middle where 'CODE WHERE' is, to show the code for something. For example: let me just print or . But it won't let me, it will convert into HTML all the time and I don't want to convert it. I've looked a lot of pages, but I don't seem to find how. Anyone a solution?
Thank you very much.
<!-- Button trigger modal -->
<div style="cursor: pointer;" class="col-md-12" data-toggle="modal" data-target="#exampleModal">
<div class="card mb-4 shadow-sm">
<div class="card-body">
Text
</div>
</div>
</a>
</div>
<!-- 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">Code</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<pre>
<code>
CODE HERE??
</code>
</pre>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close Modal</button>
</div>
</div>
</div>

First way is replacing < by < and > by > inside Your <code></code>.
Second way is make it change automatically in javascript:
var codeTags = document.querySelectorAll('code');
for (var i = 0; i < codeTags.length; i++) {
codeTags[i].innerText = codeTags[i].innerHTML;
}
Or easier in jQuery:
$('code').each(function(){
$(this).text($(this).html());
});

Related

Modal only pops up once after a refresh maybe 1 out 10 times. Why does it do this?

I have a site view modal and it pops up maybe once every ten times after a refresh. Why does it do this? Could it be interference of different JS on my page? I have checked other posts about this issue and none of their solutions have worked for me.
<a data-toggle="modal" href="#myModal" class="btn btn-default">Launch
modal</a>
<div class="modal fade" id="myModal">
<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">Write Your Review</h4>
</div>
<div class="modal-body">
[site_reviews_form assign_to="post_id"]
</div>
<div class="modal-footer">
Close
Save changes
</div>
</div>
</div>
</div>

Bootstrap modal opens after double click rather open at first click in Angular 4

I am working in an eCommerce application ,In this I have cart icon .What I want to do is when I click on the cart icon I want to show a modal screen with the user selected product data .
But in my case for the first time after page loads modal screen opens after double click,afterwards it opens with the single click .
icon :
<i class="fas fa-cart-plus fa-flip-horizontal text-primary" (click)="get_My_Cart($event)" data-toggle="modal" data-target="#exampleModal" style="font-size:25px;cursor: pointer;"></i>
Modal :
<div *ngIf="mycart && mycart.length" class="modal fade modalstyle" 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 headerHeight text-white " style="background:rgb(0, 0, 0);font-weight:bold">
<h6 class="modal-title" id="exampleModalLabel">My Cart Items</h6>
<button #closeModalBtn type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div *ngFor="let products of mycart;let i =index;">
<div class="container col-sm-12">
<div class="row">
<div class="col-sm-4 paddingforimage">
<img [src]="products['ITEM_IMAGE_PATH']">
</div>
<div class="text-info col-sm-6">
<span>
<h6>{{products?.TOTAL_QTY}} × {{products?.ITEM_PRICE}} ₹</h6>
</span>
<span>
<h6>{{products?.ITEM_DESC}}</h6>
</span>
<span>
<h6>{{products?.TOTAL_AMT}} ₹</h6>
</span>
</div>
<div class="col-sm-1 text-right">
<button type="button" class="close closebtn" aria-label="Close" (click)="detele_My_Cart(products?.ITEM_CODE)">
<span aria-hidden="true" (click)="detele_My_Cart(products?.ITEM_CODE)">×</span>
</button>
</div>
</div>
</div>
<hr>
</div>
<div class=" container row col-sm-12">
<div class="col-sm-6">
<strong>SHIPPING</strong>
</div>
<div class="col-sm-6 text-right">0 ₹</div>
<hr>
<div class="col-sm-6">
<strong>TOTAL</strong>
</div>
<div class="col-sm-6 text-right">{{my_Cart_Total_Amount}} ₹</div>
</div>
<br>
<div class="container row col-sm-12" id="wrapper">
<button type="button" class="btn btn-success buttonSize" data-dismiss="modal" routerLink="/cartsummary">
<strong>CHECK OUT</strong>
</button>
</div>
</div>
</div>
</div>
</div>
Component:
get_My_Cart($event) {
this.publicIp.v4().then(ip => {
this.CartdataService.get_My_Cart_Products(ip).subscribe(
data => {
this.mycart = data['Table']
this.my_Cart_Total_Amount = data['Table1'][0]['TOTAL_AMOUNT']
});
});
}
Here in modal screen I have a validation ,Modal screen is open if data is bind in it otherwise it will not .(modal will not open if the cart is empty).
*ngIf="mycart && mycart.length"
If I remove this validation from the modal screen it works ,but it always opens (if there is no product in cart and user clicked on the cart icon it opens but I want to restrict it .)
can anyone tell me how to handle this issue ..
The flow of code is not correct.
Anyway, I think, you're using jQuery & Bootstrap.js. (Bad Idea)
You can programmatically trigger the modal using $('#exampleModal').modal('show') once data comes from server.
declare var $ :any;
get_My_Cart($event) {
this.publicIp.v4().then(ip => {
this.CartdataService.get_My_Cart_Products(ip).subscribe(
data => {
this.mycart = data['Table']
this.my_Cart_Total_Amount = data['Table1'][0]['TOTAL_AMOUNT']
if(this.mycart && this.mycart.length)
$('#exampleModal').modal('show')
});
});
}
But there is a simple workaround in this case. Try to toggle display property of CSS instead of removing it from DOM using *ngIf.
<div [ngStyle]="{display: mycart && mycart.length ? 'block':'none'}" class="modal fade modalstyle">
</div>

How to get data data attribute values at modal box section?

I have modal box like this:
<!-- Modal FOR PLAYERS-->
<div class="modal fade" id="Info" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h3 class="modal-title" id="myModalLabel">Player profile for</h3>
</div>
<div class="modal-body info-player">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12">
<label>GOALK IN THIS SEASON :</label> 11
</div>
</div>
</div>
</div>
</div>
</div>
And i have added a Span Element where users can click on it to get the relevant data:
<span class="playerInfo" data-toggle="modal" data-target="#Info">Info</span>
but now i wan to have some "data-XYZ='VALUE'" attributes in my SPAN Element and I want to parse to my modal box, how could I do someting like this?
I only know the way with an jquery onclick handler, but i think is also another better way or what you think?
Thanks for your help or any hints...
thanks
You can make use of the bootstrap shown.bs.modal event
Occurs when the modal is fully shown (after CSS transitions have completed)
$('#Info').on('shown.bs.modal', function() {
$(".playerInfo").data("xyz"); //value
// use the above data however you want
})
HTML:
<span class="playerInfo" data-xyz="value" data-toggle="modal" data-target="#Info">Info</span>

External Page Content Overflows out of Bootstrap Modal

I have the following Modal:
<a href="somepage.htm" data-toggle="modal" data-target="#extLinkModal">
<div class="modal fade" id="extLinkModal" tabindex="-1" role="dialog" aria-labelledby="extlinkModalLabel" aria-hidden="true" data-backdrop="static">
<div class="modal-dialog " role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="extLinkModalLabel"></h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
When I click the link to launch modal, the content of somepage.htm overflows out of the modal and there are no scroll bars on the modal?
How come this is happening?
Let's say you have 2 pages, index.htm and somepage.htm
you have modal in page index.htm and you want to show somepage.htm inside modal. Then
index.htm page code will be
<a href="somepage.htm" data-toggle="modal" data-target="#extLinkModal">
<div class="modal fade" id="extLinkModal" tabindex="-1" role="dialog" aria-labelledby="extlinkModalLabel" aria-hidden="true" data-backdrop="static">
<div class="modal-dialog " role="document">
<div class="modal-content">
//Here you can show the content from `somepage.htm`
</div>
</div>
</div>
and somepage.htm page content will be
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="extLinkModalLabel"></h4>
</div>
<div class="modal-body">
//Put the page content here
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
This will resolve the issue somepage.htm overflows out of the modal and there are no scroll bars on the modal
Alternate Solution
OP asked in comments to load the remote page content into modal-body and above code example is default behavior of bootstrap v3+ where modal ignores the modal-body and always load the remote content into <div class="modal-content"> doesn't matter even if <div class="modal-body"> exist inside the <div class="modal-content">.
To get around this and make sure that the remote content load inside <div class="modal-body">
Use bootstrap modal events
Do not use href or remote in modal call button or link
So the modal call button or link will be
<a datalink="somepage.htm" data-toggle="modal" data-target="#extLinkModal" class="btn btn-primary">
where href changed to datalink or any word can be used but not href or remote otherwise modal will detect it as remote content and ignores the <div class="modal-body"> and load the content into <div class="modal-content">
Modal HTML
<div class="modal fade" id="extLinkModal" tabindex="-1" role="dialog" aria-labelledby="extlinkModalLabel" aria-hidden="true" data-backdrop="static">
<div class="modal-dialog " role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="extLinkModalLabel"></h4>
</div>
<div class="modal-body">
//Remote Page Content loads here
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
and JS
<script>
$(document).ready(function() {
$("#extLinkModal").on("show.bs.modal", function(e) {
var link = $(e.relatedTarget);
$(this).find(".modal-body").load(link.attr("datalink"));
});
});
</script>
The content of somepage.htm overflows out of the modal because your somepage.htm page is not responsive, and your modal have some fixed height and width. So try to make your page responsive using bootstrap classes and some additional css if needed.
So making somepage.htm page content responsive will solve both problems of content overflows out of the modal as well as scroll bar.
Hope it will help you!

Creating a DIV Popup Warning

On this example: HTML / CSS Popup div on text click
How can I make this load on the index page when they visit the site the first time. I don't want a button to click, I want this load automatically; and then have a close button when they read the content on the popup. Thanks in advance.
Hi you can do this using bootstrap model popup
for this include bootstrap css and js files and use below code
Your Html Code
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" arialabelledby="myModalLabel" 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">Intorduction</h4>
</div>
<div class="modal-body">
Body Text
</div>
<div class="modal-footer">
<a class="btn btn-default" data-dismiss="modal">Close</a>
</div>
</div>
</div>
</div>
then use this script
<script>
if (localStorage.getItem('showModal')!='yes')
{
localStorage.setItem('showModal','yes');
$('#myModal').modal('show');
}
</script>