Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 686
VTodorov Avatar asked Dec 05 '15 09:12

VTodorov


People also ask

How do you create a dynamic modal?

const modal = document. createElement('div'); modal. classList. add('modal'); modal.id = 'modal'; modal.

How can I get dynamic data from bootstrap modal?

Load Dynamic Content from Database in Bootstrap ModalBy clicking the Open Modal ( . openBtn ) button, the dynamic content is loaded from another PHP file ( getContent. php ) based on the ID and shows on the modal popup ( #myModal ).

How do I change the width and height of a modal?

Change the size of the modal by adding the . modal-sm class for small modals, . modal-lg class for large modals, or . modal-xl for extra large modals.

Is bootstrap modal responsive?

Bootstrap modals are supposed to be responsive by default. Something to look out for is if you have a width set on your Modal. For example I had a form body container inside of my modal with a height of 40rem.


3 Answers

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');

  });
})
like image 115
Raman Avatar answered Sep 22 '22 07:09

Raman


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');
});
like image 27
Ajay Makwana Avatar answered Sep 20 '22 07:09

Ajay Makwana


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>";
like image 23
Manoj Salvi Avatar answered Sep 20 '22 07:09

Manoj Salvi