How to change text inside modal using jquery? - javascript

I am using this modal:
<div id="myalertbox" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal">×</button>
You have a new notification!
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
Which pops up and says "You have a new notification!". I pop it up by using this script:
<script>
function showAlert() {
$("#myalertbox").modal({
"backdrop": "static",
"keyboard": true,
"show": true,
});
}
</script>
I dont know how to change the text inside though? I tried with .text and .html but it doesn't work.

Change text before calling the modal as
$("#myalertbox .modal-body").text('pass your text here');

Before initiating the modal, you can change its html like:
$("#myalertbox .modal-body").html('pass your html here');
// It will put your html inside the div having Class .modal-body which is enclosed with a div having ID #myalertbox
and after that initiate its modal as usual way.

As the content is not nested into any tags.
It's better to use this one below.
var btnHtml='< button type="button" class="close" data-dismiss="modal">
&times ;< /button>';
$('#myalertbox .modal-body').html(btnHtml +"your text here");

Related

JavaScript : Piece of code that breaks my modals

I have a piece of code that will create a contextual menu on right-click on one input in my page. The code is supposed to add "{Keyword: }" to the input. The problem is when I add the JS below all the modals that I use stop working (not displaying). The modals are activated by a button click.
Contexctual menu HTML:
<div id='cntnr'>
<ul id='items'>
<li>{Keyword:fallback}</li>
<li>{KeyWord:fallback}</li>
</ul>
</div>
Contexctual menu JS:
$("#headline11").on("contextmenu",function(e){
e.preventDefault();
$("#cntnr").css({"left": e.pageX-3, "top": e.pageY-3});
$("#cntnr").fadeIn(200, startFocusOut);
});
function startFocusOut(){
$(document).on("click",function(){
$("#cntnr").hide();
$(document).off("click");
});
}
$("#items > li").click(function(){
$("#headline11").val($(this).text().replace("fallback", " "));
var input1 = document.getElementById("headline11");
setInputSelection(input1, 9, 10);
});
Button:
<button onclick="load();setTimeout(myFunction, 10)" class="myBtn1" data-toggle="modal" data-target="#build">1 - Create campaign</button>
The modal:
<div class="container">
<!-- Modal Preview-->
<div class="modal fade" id="build" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"></button>
<h4 class="modal-title">Your Campaign Preview1</h4>
</div>
<div class="modal-body">
<div id="hoursSaved"></div>
Some text here.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
When I use {Keyword: Whatever here} in my input and submit, the modals stop working, but when I remove the JS above everything works, except the contextual menu of course.
It took me hours to debug my code and find what part of it is causing the problem, but I'm unable to understand why. Your help will be much appreciated.
Thank you,
EDIT: Here is a JsFiddle as requested in the comments. You'll have to run the code between each test to see the difference.
https://jsfiddle.net/yagami889/xpvt214o/470697/
You'll have to use the contextual menu to add the {Keyword:} you can enter whatever you want after ":", it won't work if you past it.
I found the answer. I had to use:
function startFocusOut(){
$("#cntnr").on("click",function(){
$("#cntnr").hide();
$("#cntnr").off("click");
});
}
Instead of:
function startFocusOut(){
$(document).on("click",function(){
$("#cntnr").hide();
$(document).off("click");
});
}

add dynamic onclick="location.href= to button via js

I have a working site with a working modal popup built with jquery. On this site there is a table with a column "delete". Every row has a href which opens the modal popup and in this popup there is a a href link "DELETE". This href link is filled dynamically by js code, depending on which delete column link was clicked on the site:
<td><span id="hoverdeleteTI" data-href="deleteTI.php?id=2" data-toggle="modal" data-target="#confirm-delete">OPEN POPUP</span></td>
The following script adds the href part to a href inside the modal popup.
<script>
$('#confirm-delete').on('show.bs.modal', function(e) {
$(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
$('.debug-url').html('delete URL: <strong>' + $(this).find('.btn-ok').attr('href') + '</strong>');
});
</script>
Modal popup:
<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="head-deletepopup">
<span class="close" data-dismiss="modal" aria-hidden="true">×</span>
<div class="mpopup-titel">DELETE CONFIRM</div>
</div>
<div class="modal-body">
<p>REALLY?</p>
<p class="debug-url"></p>
</div>
<div class="modal-footer">
<button type="button" class="ddeletebutt btn-default" data-dismiss="modal">CANCEL</button>
<button type="button" class="ddeletebutt btn-ok">DELETE BUTTON</button>
<a class="dddeletebutt btn-ok">DELETE</a>
</div>
</div>
</div>
</div>
The script converts this part <a class="dddeletebutt btn-ok">DELETE</a>
into <a class="dddeletebutt btn-ok" href="deleteTI.php?id=2">DELETE</a>
That's the part which is working. But what I actually want is, that it adds the href to the button before "DELETE BUTTON". Therefore I need to transcode this code <button type="button" class="ddeletebutt btn-ok">DELETE BUTTON</button>
into
<button type="button" class="ddeletebutt" onclick="location.href='deleteTI.php?id=2'">DELETE BUTTON</button>
But at the moment I obviously only getting: <button type="button" class="ddeletebutt btn-ok" href="deleteTI.php?id=2">DELETE BUTTON</button>
How can I change the added href= into onclick="location.href=
Thank you!
Change this line
$(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href'));
To this:
$(this).find('.btn-ok').attr('onclick', 'location.href=\'' + $(e.relatedTarget).data('href') + '\'');
If your code is targeting the anchor element instead of your button then use a better selector for finding the element. In your case prefix your class selector with a tag selector ie button.btn_ok
$(this).find('button.btn_ok')
Also you don't need to set the actual html onclick attribute, you can use jQuery event methods, or the native addEventListener
let href = $(e.relatedTarget).data('href');
$(this).find('button.btn_ok').click(()=>location.href=href});
//or with native methods
this.querySelector('button.btn_ok').addEventListener('click',()=>location.href=href});
And by your example your end element would not have the btn_ok class any more which you can remove by using jQuery's removeClass()
$(this).find('button.btn_ok').removeClass('btn_ok');

How to close & open a modal like Bootstrap

I want to create a modal like Bootstrap ...
I have wrote this codes :
<button type="button" data-target="#myModal" class="btn btn-primary">Open Modal</button>
<div class="modal fadeIn" id="myModal">
<div class="modal-header">
Roj Framework <span class="close-btn">×</span>
</div>
<div class="modal-content">
Hi, This Roj Framework !
</div>
<div class="modal-footer">
Continue ...
</div>
</div>
But I have a problem in understanding how to close & open it. I put a data-target for button to match with Modal Box ID & I don't display the modal from the beginning ..., So I wrote this code but It doesn't work ...
$('.modal').css('display', 'none');
$('button').click(function(){
var boxID = $(this).data('target');
var modalBox = $('.modal');
var modalBoxAttr = modalBox.attr('id');
if('boxID' == 'modalBoxAttr') {
modalBox.css('display', 'block');
}
});
There a few mistakes with your code.
Mistake 1:
Here:
if('boxID' == 'modalBoxAttr')
You are currently checking the strings and not the objects. What you have written is the following:
If the string "boxId" is equal to the string "modalBoxAttr" do something. And both strings are not the same, so you get a false result.
Mistake 2:
You are complicating the code too much.
You can just simplify it like this.
$('button').click(function(){
var boxID = $(this).data('target');
var boxObject = $(boxID);
if(boxObject.length) {
boxObject.toggle();
}
});
.modal { display: none; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" data-target="#myModal" class="btn btn-primary">Open Modal</button>
<div class="modal fadeIn" id="myModal">
<div class="modal-header">
Roj Framework <span class="close-btn">×</span>
</div>
<div class="modal-content">
Hi, This Roj Framework !
</div>
<div class="modal-footer">
Continue ...
</div>
</div>
Use more often console.log() to debug your code and check if you are actually entering a specific section and if your logic is correct.
Try following approach.
Remove data-target="#myModal" from button
<button type="button" class="btn btn-primary">Open Modal</button>
Button click:
$('button').click(function(){
$("#myModal").modal('show');
}});

how can i create a modal by remote and initial textbox in the same function using jquery?

script.js
function change_val(){
$("#remoteModal2").modal({
remote:"modals/edit_text_value.html",
show:true
});
$("#my_textbox").val("this is a text")
}
index.html
<div class="modal fade" id="remoteModal2" tabindex="-1" role="dialog" aria-labelledby="remoteModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
<button onclick="change_val()" >show modal</button>
edit_text_value.html
<div class="modal-header">
header
</div>
<div class="modal-body">
<textarea id="my_textbox"></textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">cancel</button>
</div>
The problem is that I must run it twice to make it work !
How to do all the operations in a function ?
shown.bs.modal OR show.bs.modal
When we use this code :
.on('shown.bs.modal')
.
$('#remoteModal2').on('shown.bs.modal', function (e) {
$("#my_textbox").val("this is a new text);
return e.preventDefault();
});
import modal (remote "edit_text_value.html")
Open modal
After the completion of loading and effects , change value..
show new value...
But this code :
.on('show.bs.modal')
Apply the changes ,Before reading the tags and remote the modal...
Therefore, it is not effective.
Because it can not find this ID (#my_textbox) Before remote modal page!
more information...

Passing Data to Bootstrap Modals

I am trying to create a simple modal using twitter bootstrap. The function of that modal is to take the name of the link clicked on, and use that name as a header. Each of the names are in a Django Database. I followed this link for reference: Passing data to a bootstrap modal
#Button to toggle modal
<a data-toggle="modal" data-id="{{ name }}" title="Add this item" class="open- AddBookDialog " href="#addBookDialog">{{ name }}</a><br />
#Modal
<div class="modal hide fade" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<input type="text" name="nameId" id="nameId" />
<p>some content</p>
</div>
</div>
#JavaScript
<script type="text/javascript">
$(document).on("click", ".open-AddBookDialog", function () {
var myNameId = $(this).data('id');
$("modal-body #nameId").val( myNameId );
});
</script>
What the above does is displays a text box with the characters name in the box. What I would like is to have the name as the header, not in a text box. When I remove the textbox, and place a header tag with the same id and name, nothing will show. Is there a way to complete this task.
the header is the first h3 in the modal.
So you need to add a h3 element under ".modal-header" with an id and set the .html() of that element. :)
Look here for reference: http://twitter.github.io/bootstrap/javascript.html#modals
Here's the code:
#Modal
<div class="modal hide fade" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3 class="hdrtitle">This is the header</h3>
</div>
<div class="modal-body">
<input type="text" name="nameId" id="nameId" />
<p>some content</p>
</div>
</div>
Using a code like:
$('.modal .hdrtitle').html('This is the header');
and then showing it with:
$('.modal').modal('toggle');
works.
The one-off way
If this is the only time you'll ever need to do a UI-binding behavior in your site/app, do something like this:
$('.header-link').click(function(e) {
$('.modal-header h4').html($(e.target).html())
})
CodePen Demo
This will just take the text (actually the HTML) in the link itself and put that in the header.
Using a library
If you plan to do these sorts of cool tricks in more parts of your app, consider using a UI-binding library like Angular JS or Knockout. This way you can data-bind parts of your UI with events.
I found the answer. Thanks to #PaoloCasciello for a layout.
Instead of an .on function in javascript, it really needed to be .html. So here is what the final code will look like.
<!-- Modal -->
<div class="modal hide fade" id="addBookDialog">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3 id="nameId" />
</div>
<div class="modal-body">
<p>some content</p>
</div>
</div>
<script type="text/javascript">
$(document).on("click", ".open-AddBookDialog", function () {
var myNameId = $(this).data('id');
var myNameDescription = $(this).data('description');
$(".modal-header #nameId").html( myNameId );

Categories

Resources