Bootstrap modal stopping checkbox from selecting - javascript

I have a checkbox on the bottom of my page. When the user clicks it, I want a popup to show up. Everything works except the toggling of the checkbox. When I click the checkbox using the code below the pop up appears but the box does not check. Any idea why?
The jQuery:
$('#confirm-final').on('show.bs.modal', function (e) {
$(this).find('.danger').attr('href', $(e.relatedTarget).data('href'));
});
The checkbox:
<label class="checkbox-inline"><asp:CheckBox ID="chkFinalSubmission" runat="server" data-toggle="modal" data-target="#confirm-final"/>Final Submission</label>
The popup:
<div class="modal fade" id="confirm-final" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
Final Submission
</div>
<div class="modal-body">
If you check final submission and submit this ad note, <strong>you will not be able to change it anymore.</strong> It will be permanently stuck in readonly mode.
</div>
<div class="modal-footer">
<asp:Button ID="btnConfirmFinal" runat="server" Text="Got it" class="btn btn-default" data-dismiss="modal"/>
</div>
</div>
</div>

As for the why, the event that is fired to open a modal contains e.preventDefault(), which cancels any event that might have occurred by clicking the modal-invoking element. If it's a link, the href doesn't get followed but the modal opens instead. If it's a checkbox, the actual check is cancelled. This is built-in behavior, and you can't prevent it.
How to solve it:
In Bootstrap 3 you have access to the relatedTarget property of the event that fired the modal. Since your function already uses relatedTarget I'm assuming you use Bootstrap 3 indeed. You could just add the following rule to your existing jquery function:
$(e.relatedTarget).prop('checked', !$(e.relatedTarget).prop('checked'));
(This sets the checkbox to the inverse of the already existing 'checked' state, aka it toggles it on and off everytime you click).
Also, are you sure the rest of your code works? You refer to $(e.relatedTarget).data('href') but your checkbox has no data-href. Did you mean data('target') instead?

Related

jQuery Modal only Opened once after clicked

i've some problem that make make head feels so heavy... i've searched for the answer in this forum, but the problem still not solve.
my problem is i've a button that link to modals. when first time after the page refreshed the modal open while the button clicked. but not for the second time.
i've check the log and the log said
TypeError: $(...).modal is not a function[Learn More].
if i refreshed the page, it will only work once, and the second click of the button, the modals not open again...
this is my button HTML view code..
<button class="btn btn-info btn-sm btn-labeled" type="button" id="tambahUser" data-toggle = "modal" value="<?= Url::to(['/module/create'],true)?>">
this is my modal
<div id="modalSignUpSm" tabindex="-1" role="dialog" class="modal fade">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header bg-info">
<button type="button" class="close" data-dismiss="modal" id="tutupModal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title">Colored Header Modal</h4>
</div>
<div class="modal-body">
<div id="modalContent"></div>
</div>
</div>
</div>
</div>
and this is my js code that responsible for handling click event of the button
$('#tambahUser').click(function(event){
$('#modalSignUpSm').modal({
backdrop: 'static',
keyboard: false,
})
.modal('show')
.find('#modalContent')
.load($(this).attr('value'));
});
$('#tutupModal').click(function(){
$('#modalSignUpSm').modal('close');
});
this is my controller (i'm using Yii2) of Module/create
public function actionCreate()
{
$model = new Module();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(Url::to(['/paneladm/pengaturan/module',true]));
} else {
return $this->renderAjax('create', [
'model' => $model,
]);
}
}
My Question is why the modals only open once and after that, the button stuck..? how to resolve that...
Note: - i'm new on jQuery, and i will very thanks for every answer.
- i've search on this forum and try to resolve with create a button and every this button clicked will be firing the .modal('close') but still not work
You should either use Bootstraps data-attributes to trigger the modal on and off, or use trigger via javascript manually. Right now you are trying to do both. See this example.
So I would remove the bootstrap data-attributes and then make sure your bootstrap script is loaded before your own javascript.

Question mark in URL after closing alert box

I have a dummy Bootstrap modal with a very simple JS alert meant to be triggered when the submit button is clicked. The code is live here and this is what it looks like:
<div class="modal fade" id="contact" role="dialog" >
<div class="modal-dialog">
<div class="modal-content">
<form class="form-horizontal" role="form" name="contact-form">
<div class="modal-header">
<h4>test</h4>
</div>
<div class="modal-body"><p>This is body</p></div>
<div class="modal-footer">
<button type="submit" id="submit" class="btn btn-primary btn-lg" onclick="alert('something');">Send</button>
</div>
</form>
</div>
</div>
If you visit the site, you can trigger the modal by clicking on the contact link in the top navigation menu. The modal looks like this:
As you can see, there's just one field and a submit button. The button's onclick() event is set to alert the word "something" on the screen. This works fine except that when you close the alert, the page refreshes with a "?" appended to the URL. How do I prevent this refresh and where does the question mark come from?
At first I thought that it happens because the button has property type set to submit, so I'd recommend to remove this property completely, and the trailing question mark would probably not appear anymore. But it does.
What really needs to be done is event default action has to be prevented. To do that, return false right there in the onclick event callback function:
<button onclick="alert('something'); return false;">...</button>
This way you, well, return false, and this is perceived as if you want to prevent further execution of the click event, effectively submit operation.

How to hide a modal and show another one using jquery?

I open a modal, then on click of a div within the modal I want to close that modal and open another one. But when I do this it closes the first modal and only shows the background of the second modal with the body not displayed.
My HTML is:
<div id="test-modal" class="modal hide fade" data-keyboard="true">
<div class="modal-body">
<div id="option"><p>Click here to show the next modal</p></div>
</div>
</div>
<div id="test-modal2" class="modal hide fade" data-keyboard="true">
<div class="modal-body">
<p>modal show after a hide doesn't work?</p>
</div>
</div>
and my jquery is:
$('.option').click(function() {
$('#test-modal').modal('hide');
$('#test-modal2').modal('show');
});
This can help:
Remove hide class from modals divs
You use .option in your selector, but in html you use id="option", so change them
Working version
My Stupid mistake the jquery works. I accidently didnt close off the first modal properly which is why the animation wouldnt finish and cause the second modal to not load. I simply put the secondary modal on top of the first modal to test and it worked.
Please try using this data-dismiss attribute on close button.
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>

How to change content of a modal at each opening

I have'a modal ( I use twitter bootstrap 2.3.2 and I need to use it please dont say to use bootstrap3), and I want to show different content based on the clicked button. In other words, there are multiple buttons that are added dynamically into dom, and based on the clicked button, I need to show different content. Currently, it always shows the content of the first clicked button no matter where I'am pushing. Here is my code:
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button>
<h3 id="myModalLabel">Query Name</h3>
</div>
<div class="modal-body">
<div id="graph" style="position:relative;overflow:hidden;cursor:default;" class="{{getViewType}}">
<center id="splash" style="padding-top:230px;">
<img src="../images/loading.gif">
</center>
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
And this is my handler:
$(document).on('hidden','#myModal',function(){
console.log('it comes here'); // this is printed every time I close modal
$(this).removeData('modal');
});
Perhaps check that the id's of the dynamically added buttons are unique. Also that any function code that will run when they're clicked, is also dynamically added otherwise it won't run. It would certainly help to see the code for the buttons.
DEMO
Ok.. I prefer .shown event of modal for this case and a global variable to store the data of clicked button as below
var dataToShow=""; //global variable
//A button click event to store data
$('button').on('click',function(){
dataToShow=$(this).text();//For DEMO purpose am storing text of button clicked in dataToShow
});
$(document).on('shown','#myModal',function(){
//Will be triggered when modal is opened
alert(dataToShow);//Just for verification
$('.modal-body .datadisplay').html(dataToShow);
//assign the value in a separate div inside modal-body so that you will not replace anything else each time
});
Now since the button is dynamically created, I would suggest to use event delegation here to attach event to button
$(document).on('click','.yourbuttonClass',function(){
dataToShow=$(this).text();
});
Now if you want you can make use of
$(document).on('hidden',"#myModal",function(){
//clear the contents inside an element added i.e. .datadisplay inside modal-body
$('.modal-body .datadisplay').empty();
})

Show DIV element in Twitter Bootstrap Modal along with DOM

I have a simple div tag, that contains a button. When ever user clicks on this button it simply shows an alert.
<div id="myDiv">
<input type="button" id="btn" value="Click Me" />
</div>
and I also have a empty twitter bootstrap modal.
<div class="modal hide" id="myModal">
<div class="modal-header">
<button class="close" data-dismiss="modal">×</button>
<h3>Modal header</h3>
</div>
<div class="modal-body"></div>
</div>
I have a second button, when ever the user clicks on it, it opens the bootstrap modal. I want to show "myDiv" in bootstrap modal when ever the user clicks on the second button. I want "myDiv" to be present in when ever modal opens and also I want to be present in my HTML document. So that I can always access it with out creating second button in modal.
Any idea how can I do that ?
I think this is what you were going for so basically on the modal show we append that button to the modal body.
$("#myDiv") .appendTo(".modal-body");
but we aren't done because after modal close we need to get it back in the body so we do:
$('#myModal').on('hide.bs.modal', function (e) {
$("#myDiv") .prependTo("body");
})
Here is a working fiddle:
http://jsfiddle.net/zcb3h/2/
I hope this is what you were looking for.

Categories

Resources