最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to change text inside modal using jquery? - Stack Overflow

programmeradmin1浏览0评论

I am using this modal:

 <div id="myalertbox" class="modal fade">

    <div class="modal-dialog">

        <div class="modal-content">

            <div class="modal-body">

                <button type="button" class="close" data-dismiss="modal">&times;</button>
                You have a new notification!

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>

</div>

Which pops up and says "You have a new notification!". I pop it up by using this script:

<script>

function showAlert() {
    $("#myalertbox").modal({

        "backdrop": "static",
        "keyboard": true,
        "show": true,
    });
}

 </script>

I dont know how to change the text inside though? I tried with .text and .html but it doesn't work.

I am using this modal:

 <div id="myalertbox" class="modal fade">

    <div class="modal-dialog">

        <div class="modal-content">

            <div class="modal-body">

                <button type="button" class="close" data-dismiss="modal">&times;</button>
                You have a new notification!

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>

</div>

Which pops up and says "You have a new notification!". I pop it up by using this script:

<script>

function showAlert() {
    $("#myalertbox").modal({

        "backdrop": "static",
        "keyboard": true,
        "show": true,
    });
}

 </script>

I dont know how to change the text inside though? I tried with .text and .html but it doesn't work.

Share Improve this question asked Jun 13, 2017 at 5:44 Gimv13Gimv13 1632 gold badges3 silver badges16 bronze badges 2
  • try this,, $('#myalertbox .modal-body').text('what text you want'); before load modal , but i think will be done by bootstrap better – Álvaro Touzón Commented Jun 13, 2017 at 5:46
  • thanks, works perfectly :) – Gimv13 Commented Jun 13, 2017 at 5:48
Add a ment  | 

3 Answers 3

Reset to default 5

Change text before calling the modal as

$("#myalertbox .modal-body").text('pass your text here');

Before initiating the modal, you can change its html like:

$("#myalertbox .modal-body").html('pass your html here');
// It will put your html inside the div having Class .modal-body which is enclosed with a div having ID #myalertbox

and after that initiate its modal as usual way.

As the content is not nested into any tags.

It's better to use this one below.

 var btnHtml='< button type="button" class="close" data-dismiss="modal"> 
 &times ;< /button>';

 $('#myalertbox .modal-body').html(btnHtml +"your text here");
发布评论

评论列表(0)

  1. 暂无评论