<...">

How to dynamically change the boot modal case

So, I have a 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 an empty body using jscript. I am a beginner in Javascript, so I will take care of the simplest answer.

+4
source share
3 answers

Try the following:

$('.modalShow').click(function(event){
    event.preventDefault();
    var e = $(this);
    var title = e.data('title');
    var body = e.data('value');
    $("#myModal").modal("show");
    $('#modal-title').html(title);
    $('#modal-body').html(body);
});
+3
source

Please see this script.

Here is the code:

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

  });
})
+6
source

The simplest solution is you can use javascript innerHTMLto modify the html of your modal body as follows:

//get your modal body by class and change its html
document.getElementByClass("modal-body").innerHTML = "<p>some text</p>";
+2
source

Source: https://habr.com/ru/post/1618683/


All Articles