Bootstrap Global Modal Code

Use this link and modal HTML to utilize a single modal window for multiple uses. Data attributes are used in the link to provide the title, button text and button ID of the modal (can be used to target click functions on save/close, etc.)

by revive

HTML

<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap.min.css">
<script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/js/bootstrap.min.js"></script>
				<a href="http://fiddle.jshell.net/Sherbrow/bHmRB/0/show/"
					data-modal-size="used-by-CSS-to-change-modal-size-position"
					data-modal-title="Modal Title"
					data-modal-class="custom-js-or-css-for-this-modal"
					data-button-id="ID-for-save-button"
					data-modal-button="text-for-modal-save-button"
					data-target="#default-modal"   
					class="btn btn-info open-modal">Button Text</a>	

<div id="default-modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="Modal Window" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">Ă—</button>
        <h3 class="myModalLabel">Information</h3>
    </div>
    <div class="modal-body"></div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" id="cancel" aria-hidden="true">Close</button>
        <button class="btn btn-primary modalsave" id="">Save</button>
    </div>
</div>

JavaScript

$('.open-modal').click(function(e) {
	    e.preventDefault();
		var url = $(this).attr('href'),
			modalsize = $(this).data('modal-size'),
			modaltitle = $(this).data('modal-title'),
	       	modalclass = $(this).data('modal-class'),
	        buttonid = $(this).data('button-id'),
	        modalbutton = $(this).data('modal-button'),
	 		target = $(this).data('target');
			
        //set the text of the modal title
        $(target+' .myModalLabel').html(modaltitle);

		//add class to modal, if needed, to override modal size/placement
		if(modalclass){$(target).addClass(modalclass);}
		
        // set the button id so we can target the click function of it.
		if(buttonid){$(target+' .modalsave').attr("id",buttonid); }

		if(modalsize){$(target).addClass(modalsize); }

        // set the text of the save button 
		if(!modalbutton){
		   $(target+' .modalsave').hide(); 
		} else {
		  $(target+' .modalsave').show().html(modalbutton);   
		}

		$(target).find('.modal-body').load(url, '', function(){
			$(target).modal().on('hidden', function(){ 
				$(this).find('.modal-body').empty();
				 // lets clear the classes and settings we just applied, so concurrent modals dont retain these!
				if(modalclass){$(target).removeClass(modalclass);}
				if(modalsize){$(target).removeClass(modalsize);}
				$(target+' .myModalLabel').html('Information');
				$(target+' .modalsave').html('Save');
				$(target+' .modalsave').removeAttr("id");
			});
		});	
	});