VTodorov
VTodorov

Reputation: 973

How to dynamically change bootstrap modal body

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">&times;</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

Answers (4)

Ajay Makwana
Ajay Makwana

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

Chamila Maddumage
Chamila Maddumage

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">&times;</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

Manoj Salvi
Manoj Salvi

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

Raman
Raman

Reputation: 1281

Please have a look at this fiddle.

Here is the code:

$(function(){
  $('.custom-modal').click(function(e){
    e.preventDefault();
    var mymodal = $('#myModal');
    mymodal.find('.modal-body').text('hello');
    mymodal.modal('show');

  });
})

Upvotes: 18

Related Questions