How to close popup modal after the checkbox is checked and then the button is clicked?
If checkbox is not checked then not to close modal. Checkbox and button placed inside modal. When I checking the checkbox and clicking the button modal is not closing.
$(document).ready(function(){
$('.one:checked').on('click' , '.close_model' , function(){
dialog('close');
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<button type="button" class="close_model btn btn-danger">button</button>
<input type="checkbox" name="ch" class="one">
<input type="checkbox" name="ch" class="one">
<input type="checkbox" name="ch" class="one">
<input type="checkbox" name="ch" class="one">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Modal Example</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<input type="hidden" name="txt1" id="textbox1">
<button type="button" class="close_model btn btn-danger">button</button>
<input type="checkbox" name="ch" class="one" value="1">
<input type="checkbox" name="ch" class="one" value="2">
<input type="checkbox" name="ch" class="one" value="3">
<input type="checkbox" name="ch" class="one" value="4">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$(".close_model").on("click",function(){
if($('#textbox1').val() == 'true'){
$('.close').trigger('click');
}else{
alert('pls click on checkbox');
}
});
$('.one').change(function() {
if(this.checked) {
$(this).prop("checked", 'true');
}
$('#textbox1').val(this.checked);
});
});
</script>
</body>
</html>
change close button code like this
<button type="button" class="btn btn-default close">Close</button>
and then add this JQuery code
$('.close').click(function(){
if ($('.one').is(':checked'))
{
$("#myModal").modal('toggle');
}
})
here you go working sample
Hi there create a function and ad the function to button click event.
<button type="button" class="close_model btn btn-danger" onclick="CheckChecked();">button</button>
Function CheckChecked()
{
var x = document.getElementById("ch").checked;
if (x)
{
$('#myModal').modal('hide');
}
}
$("#selectItem").click(function(){
var value = $("input[type='radio']:checked").val();
$('#town').val(value);
$("#myModal").modal('hide');
});
<button type="button" id="selectItem" name="selectItem" class="btn btn-primary">Select</button>
The above code worked for me
Related
I have the following code copy-pasted from w3schools Modal training page .
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Activate Modal with JavaScript</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" id="myBtn">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () { $("#myBtn").click(function () { $("#myModal").modal('show'); }); });
</script>
</body>
</html>
I am using MS Visual studio 2019, with Jquery nuget package installed in the project. I simply copy the code to test it in the razor blank page, execute , then I get nothing apart from the button, when it is clicked , no modal appears.
Nevertheless, that code works for the online html compilers.
Am I missing something here ?
try this
$(document).ready(function(){ $("#myBtn").click(function(){ $("#myModal").modal('show'); }); });
Try to use bootstrap custom attribute to trigger the modal like below:
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" id="myBtn">Open Modal</button>
use data-toggle and data-target in button tag.
I have edited your code. use data-toggle="modal" and data-target="#YOUR_MODAL_ID" in the button to make it work.
Here YOUR_MODAL_ID=myModal
Example using from HTML tag
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Activate Modal with JavaScript</h2>
<!-- Trigger the modal with a button -->
<button
type="button"
class="btn btn-info btn-lg"
data-toggle="modal"
data-target="#myModal"
id="myBtn"
>
Open Modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
×
</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-default"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Example using with Javascript (jQuery)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Activate Modal with JavaScript</h2>
<!-- Trigger the modal with a button -->
<button
type="button"
class="btn btn-info btn-lg"
id="myBtn"
>
Open Modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
×
</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-default"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function(){ $("#myBtn").click(function(){ $("#myModal").modal('show'); }); });
</script>
</body>
</html>
Problem solved :
Just added :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
Now the modal is show upon clicking the button.
The code works on Visual studio and looks like this now :
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<div class="container">
<h2>Activate Modal with JavaScript</h2>
<!-- Trigger the modal with a button -->
<button type="button"
class="btn btn-info btn-lg"
id="myBtn">
Open Modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
×
</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button"
class="btn btn-default"
data-dismiss="modal">
Close
</button>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () { $("#myBtn").click(function () { $("#myModal").modal('show'); }); });
</script>
</body>
</html>
all I am trying to show the calendar and input box when the user clicked on the radio button.
I have made one modal popup by using below code and
when I click on the send button it will open a modal popup with two radio buttons and when I click on the close popup with a reminder
button it will show calendar as well as input box.
Here is my code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Send</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<input type="radio" name="reminder">Close popup with reminder </br>
<input type="radio" name="reminder">Close popup without reminder
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Can anyone help me out how can I do that.
Here is the image reference
:
Make use of the bootstrap grid inside the modal then add change events to the radio button; if the radio button has a true value, it would show the .note div which contains the reminder, else hide it.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Send</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-xs-6">
<input class="radio-reminder" type="radio" name="reminder" value="true" /> Close popup with reminder
</div>
<div class="col-xs-6 date" style="display:none;">
<input class="form-control" type="date" />
</div>
</div>
<div class="row note" style="display:none;">
<div class="col-xs-12">
<br>
<input class="form-control" placeholder="Reminder" />
</div>
</div>
<div class="row no-reminder">
<div class="col-xs-6">
<br/>
<input class="radio-reminder" type="radio" name="reminder" value="false" /> Close popup without reminder
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$(".radio-reminder").change(function() {
if ($(this).val() == "true") {
$(".note").show();
$(".date").show();
$(".newSelect").remove();
} else {
$(".note").hide();
$(".date").hide();
var newSelect = $("<div class='col-xs-6 newSelect'><br><select class='form-control'><option>1</option><option>2</option></select></div>")
$(newSelect).appendTo($(".no-reminder"));
}
});
});
</script>
</body>
</html>
At first, you have to include the input elements in the HTML. Then you can use jQuery to hide/show those elements based on the value of the clicked radio button.
Please note that how I have added value attribute to the radio buttons.
$('input[name=reminder]').click(function(){
if($(this).val() == "1")
$('#inputDate, #inputText').css({'display': 'block'});
else
$('#inputDate, #inputText').css({'display': 'none'});
});
input[type=date]{
float: right;
}
#inputDate, #inputText{
display: none;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Send</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<input type="radio" name="reminder" value="1">Close popup with reminder <input type="date" id="inputDate"/>
<br>
<input type="text" id="inputText"/>
<br>
<input type="radio" name="reminder" value="2">Close popup without reminder
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
use below code snippet
$('input[type=radio]').on('change', function() {
if($(this).val() == 'calendar'){
$('#date-input').show();
$('#calendar').show();
$('#selectGroup').hide();
}else{
$('#date-input').hide();
$('#calendar').hide();
$('#selectGroup').show();
}
});
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Send</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<input type="radio" name="reminder" value="calendar"> Close popup with reminder
<input type="date" id="calendar" style="display: none;" />
<br/>
<input type="text" value="" id="date-input" style="display: none;" />
<br/>
<input type="radio" name="reminder"> Close popup without reminder
<br/>
<div class="form-group" id="selectGroup" style="display: none;">
<label for="sel1">Select list:</label>
<select class="form-control" id="sel1">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
I wanted to show the further details of my card in modal when the user click the button "More Details" ,i have seen an answer regarding to this however I don't want the information to be inside an input field. Can please someone help me with it?
<button class="open-homeEvents btn btn-primary" data-id="2014-123456" data-toggle="modal" data-target="#modalHomeEvents">More Details</button>
MODAL
<div id="modalHomeEvents" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="height:50px;">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<label>Student ID</label>
<input type="hidden" name="eventId" id="eventId"/>
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" value="Login" name="login" style="background-color:rgb(0,30,66); ">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
Javascript
<script type="text/javascript">$(document).on("click", ".open-homeEvents", function () {
var eventId = $(this).data('id');
$(".modal-body #eventId").val( eventId );
});</script>
Instead of using an input field you could create a container element for the id, a div or span and update content of the element using .html() jQuery method.
$(document).on("click", ".open-homeEvents", function () {
var eventId = $(this).data('id');
$('#idHolder').html( eventId );
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Modal</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>
<body>
<button class="open-homeEvents btn btn-primary" data-id="2014-123456" data-toggle="modal" data-target="#modalHomeEvents">More Details</button>
<div id="modalHomeEvents" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="height:50px;">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<label>Student ID</label>
<input type="hidden" name="eventId" id="eventId"/>
<span id="idHolder"></span>
</div>
<div class="modal-footer">
<input type="submit" class="btn btn-primary" value="Login" name="login" style="background-color:rgb(0,30,66); ">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>
I want to create a confirm box before insert any information on my database
I want to get a confirm message on clicking Add. If the user selects 'Ok' then Add is done, else if 'Cancel' is clicked nothing happens.
this is my html5 code
<script>
var option = "êtes-vous sûr de vouloir ajouter ce RDV?";
$('#btnAdd').on('click', function(e){
confirmDialog(option, function(){
//My code to Add
});
});
function confirmDialog(message, onConfirm){
var fClose = function(){
modal.modal("hide");
};
var modal = $("#confirmModal");
modal.modal("show");
$("#confirmMessage").empty().append(message);
$("#confirmOk").one('click', onConfirm);
$("#confirmOk").one('click', fClose);
$("#confirmCancel").one("click", fClose);
}
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Add RDV</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
<!-- Modal dialog -->
<div id="frmTest" tabindex="-1">
<!-- CUTTED --><div class="modal-body">
<form method="post" id="insert-form">
<label>Nom</label>
<input type="text"id="name" class="form-control" required/>
<label>Prenom</label>
<input type="text"id="lname" class="form-control" required/>
<label>Date</label>
<input type="text"id="date" class="form-control" required/>
<label>Heure</label>
<input type="text"id="heure" class="form-control" required/>
</form>
</div>
<div id="step1" class="modal-footer">
<button type="button" class="glyphicon glyphicon-erase btn btn-default" id="btnAdd"> Add</button>
</div>
</div>
<!-- Modal confirm -->
<div class="modal" id="confirmModal" style="display: none; z-index: 1050;">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"style="background-color:green;color:white;text-align:center">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title" >Confirmation!</h4>
</div>
<div class="modal-body" id="confirmMessage">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-info" id="confirmOk">Ok</button>
<button type="button" class="btn btn-danger" id="confirmCancel">Cancel</button>
</div>
</div>
</div>
</div>
</body>
</html>
my php code is in the same page with my html and javascript code how shall i pricise to javascript which code need to execute
You aren't adding the event listeners:
$("#confirmOk").one('click', onConfirm);
It should be:
$("#confirmOk").on('click', onConfirm);
Also, onConfirm function is undefined.
I want to slide some content inside modal window when a button is clicked. The effect which I want is a sample slider like effect which can be found here:
I tried to do width toggle but unfortunately it results different from I expect and more important is that the height of the modal changes when content slides. I need to be simply just one content slide out and the other slides in. JQuery UI has an slide effect But I do not want to use that because of technical details. I want to implement a JQuery + CSS solution.
Can anyone help me please? Here is the code I developed.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<div class="first-content">
<p>Some text in the modal.</p>
<button type="button" class="btn btn-info first-button">First Button</button>
</div>
<div class="second-content" style="display:none">
<p>Second content</p>
<button type="button" class="btn btn-info">Second Button</button>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>
JS:
$(function () {
$('.first-button').on('click', function () {
$('.first-content').animate({width: "toggle"}, 800);
$('.second-content').delay( 800 ).animate({width: "toggle"});
});
$('.second-content').on('click', function () {
$('.second-content').animate({width: "toggle"});
$('.first-content').delay( 800 ).animate({width: "toggle"}, 800);
});
});
Update to previous answer.
This will give you a slide in left/right module for the modal overlay.
You can tweek the main overlay height setting.
https://jsbin.com/wolijunogu/edit?html,css,js,output
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<button type="button" class="btn btn-info openModal" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<div class="first-content">
<p>Some text in the modal.<br>test for different height<br>test for different height</p>
<button type="button" class="btn btn-info first-button">First Button</button>
</div>
<div class="second-content" style="display:none">
<p>Second content</p>
<button type="button" class="btn btn-info second-button">Second Button</button>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</body>
</html>
CSS:
.modal-body div p{
overflow: hidden;
}
.first-content{
width: 100%;
float:left;
}
.second-content{
width: 100%;
float: right;
}
Javascript:
$(function () {
$(".openModal").click(function(){
setTimeout(function(){
var h=$(".modal-body .first-content p").height();
$(".modal-body").css('height',h+80+'px');
$(".modal-body .first-content p").css('height',h+'px');
},250);
});
$('.first-button').on('click', function () {
$('.first-content').animate({width:"toggle"}, function(){
$('.second-content').animate({width:"toggle"});
var h=$(".modal-body .second-content p").height();
$(".modal-body").css('height',h+80+'px');
$(".modal-body .second-content p").css('height',h+'px');
});
});
$('.second-button').on('click', function () {
$('.second-content').animate({width:"toggle"},function(){
$('.first-content').animate({width:"toggle"});
var h=$(".modal-body .first-content p").height();
$(".modal-body").css('height',h+80+'px');
$(".modal-body .first-content p").css('height',h+'px');
});
});
});