I've got a couple of hyperlinks that each have an ID attached. When I click on this link, I want to open a modal ( http://twitter.github.com/bootstrap/javascript.html#modals ), and pass this ID to the modal. I searched on google, but I couldn't find anything that could help me.
This is the code:
<a data-toggle="modal" data-id="@book.Id" title="Add this item" class="open-AddBookDialog"></a>
Which should open:
<div class="modal hide" id="addBookDialog">
<div class="modal-body">
<input type="hidden" name="bookId" id="bookId" value=""/>
</div>
</div>
With this piece of code:
$(document).ready(function () {
$(".open-AddBookDialog").click(function () {
$('#bookId').val($(this).data('id'));
$('#addBookDialog').modal('show');
});
});
However, when I click the hyperlink, nothing happens. When I give the hyperlink a href="#addBookDialog", the modal opens just fine, but it does't contain any data.
I followed this example: How to pass values arguments to modal.show() function in Bootstrap
(and I also tried this: How to set the input value in a modal dialogue?)
Your code would have worked with correct modal html structure.
You can try simpleBootstrapDialog. Here you can pass title, message, callback options for cancel and submit etc...
Here is a cleaner way to do it if you are using Bootstrap 3.2.0.
Link HTML
Modal JavaScript
http://jsfiddle.net/k7FC2/
I think you can make this work using jQuery's .on event handler.
Here's a fiddle you can test; just make sure to expand the html frame in the fiddle as much as possible so you can view the modal.
http://jsfiddle.net/Au9tc/605/
HTML
JAVASCRIPT
If you are on bootstrap 3+, this can be shortened a bit further if using Jquery.
HTML
JQUERY
Here's how I implemented it working from @mg1075's code. I wanted a bit more generic code so as not to have to assign classes to the modal trigger links/buttons:
Tested in Twitter Bootstrap 3.0.3.
HTML
JAVASCRIPT