Reputation: 973
So I have Bootstrap modal defined as:
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Error</h4>
</div>
<div class="modal-body">
<p> </p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
And I want to edit the empty body with jscript. Im a beginner in Javascript so I'll appreciate the simplest answer.
Upvotes: 11
Views: 67726
Reputation: 2372
Try this one:
$('.modalShow').click(function(event){
event.preventDefault();
var e = $(this);
var title = e.data('title');
var body = e.data('value');
$('.modal-title').html(title);
$('.modal-body').html(body);
$('#myModal').modal('show');
});
Upvotes: 14
Reputation: 3866
I have done this in C# as follows.
C# :
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
stringBuilder.Append(@"<script language='javascript'>");
stringBuilder.Append(@"$('#errorModal').find('.modal-body').text('"+ your error message + "');");
stringBuilder.Append(@"$('#errorModal').modal('show');");
stringBuilder.Append(@"</script>");
ClientScript.RegisterStartupScript(this.GetType(), "JSScript", stringBuilder.ToString());
HTML :
<div class="modal fade" id="errorModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p> </p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
Upvotes: 0
Reputation: 2739
the simplest solution is - you can use javascript's innerHTML
to change html of your modal body like this -
//get your modal body by class and change its html
document.getElementByClass("modal-body").innerHTML = "<p>some text</p>";
Upvotes: 3