-
-
Save sahinsf/3006768 to your computer and use it in GitHub Desktop.
Dynamic (AJAX) loaded Bootstrap Modal (Bootstrap 2.0)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
// Support for AJAX loaded modal window. | |
// Focuses on first input textbox after it loads the window. | |
$('[data-toggle="modal"]').click(function(e) { | |
e.preventDefault(); | |
var href = $(this).attr('href'); | |
if (href.indexOf('#') == 0) { | |
$(href).modal('open'); | |
} else { | |
$.get(href, function(data) { | |
$('<div class="modal" >' + data + '</div>').modal(); | |
}).success(function() { $('input:text:visible:first').focus(); }); | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="/url/to/load/modal_window.htm" data-toggle="modal">link</a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="modal-header"> | |
<a class="close" data-dismiss="modal">×</a> | |
<h3>Modal header 2</h3> | |
</div> | |
<div class="modal-body"> | |
<p>One body...</p> | |
</div> | |
<div class="modal-footer"> | |
<a class="btn btn-primary">Save Changes</a> | |
<a class="btn" data-dismiss="modal">Close</a> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--Citing from: http://blog.assimov.net/blog/2012/03/09/ajax-content-in-twitter-bootstrap-modal/ --> | |
<a class="btn" id="demo" href="/url/to/load/ajax.php" data-toggle="modal" data-target="#myModal">Launch Modal</a> | |
<div class="modal fade" id="myModal"> | |
<div class='modal-header'> | |
<button type='button' class='close' data-dismiss='modal'>×</button> | |
<h3>A Header</h3> | |
</div> | |
<div class='modal-body'> | |
<p>Loading...</p> | |
</div> | |
<div class='modal-footer'> | |
<a href='#' class='btn' data-dismiss='modal'>Close</a> | |
</div> | |
</div> | |
<script> | |
($("a[data-toggle=modal]")).click(function() { | |
var target, url; | |
target = $(this).attr('data-target'); // we could do: target = $("#myModal div.modal-body"); | |
url = $(this).attr('href'); | |
return $(target).load(url); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is what we've put together, to make the code more re-usable (applying ID to save button for form submit functions, Modal Titles, etc..)
THE JS:
THE LINK:
Here is an example:
http://jsfiddle.net/revive/rmjqq/
Note, that you can change the data-target of the link, for instances where you may be wanting to call a different modal HTML markup
Thanks for the inspiration and direction for this !!