I've been using Zurb's Foundation for a while and recently came across an instance where I needed to:
1) load content for the modal via AJAX (np there, easy enough)
2) Add an element to the modal to contain a dynamic title
What happens, when I code a reveal to have, say, an H3 for a title.. the AJAX call overwrites everything except the reveal-close link within the modal HTML.
What I am aiming to do is set the url, title and other options for the modal within the links data attributes, like this:
<a href="javascript:void(0);"
data-target-url="/users/view.php?member_id={$conn->id}"
data-modal-title="Contact Information"
data-modal-class="large"
data-button-id="modalButton1"
data-button-txt="CLICK ME"
class="bb-modal">
<img src="/users/avatar.php?member_id={$conn->id}" alt="" />
</a>
Here is the modal HTML code:
<div id="bb-modal" class="reveal-modal" data-reveal>
<h3 class="modal-title">Information</h3>
<div class="modal-body"></div>
<div class="modal-footer">
<button class="btn btn-primary modalsave" id="">Save</button>
</div>
<a class="close-reveal-modal">×</a>
</div>
And here is the JS I am using, to set VARS to the data attributes and then to load the content manually into the .modal-body div:
$('body').on('click','.bb-modal',function(e) {
e.preventDefault();
var targeturl = $(this).data('target-url'),
modaltitle = $(this).data('modal-title'),
modalclass = $(this).data('modal-class'),
buttonid = $(this).data('button-id'),
buttontxt = $(this).data('button-txt');
//set the text of the modal title
$('#bb-modal .modal-title').html(modaltitle);
//add class to modal, if needed, to override modal size/placement
$('#bb-modal').addClass(modalclass);
// set the text of the save button
$('#bb-modal .modalsave').html(buttontxt);
// set the button.modalsave id so we can target the click function of it.
$('#bb-modal .modalsave').attr("id",buttonid);
$('#bb-modal .modal-body').load(targeturl,function(response, status, xhr) {
if (status === 'error') {
$(target+ '.modal-body').html('<h2>Oh No!</h2><p>Sorry, but there was an error:' + xhr.status + ' ' + xhr.statusText+ '</p>');
}
}).foundation('reveal', 'open'); // end load
}); // end on click
There has GOT TO BE a cleaner, more efficient way to make this work.. so I can set the title, button text/ID, etc via the links data attributes and then load the modal without Foundations Reveal JS overwriting ALL the content in the modal...
Any help is GREATLY appreciated!.... as of now, my desk has a large dent, and my head is killing me :D