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

javascript - innerHTML in bootstrap modal - Stack Overflow

programmeradmin1浏览0评论

I am now spending hours on one thing. innerHTML changes div or

javascript code:

function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}

Modal:

<!-- Template -->
<div id="faqask" class="modal fade" tabindex="-1" role="dialog" style="display: none;">
    <div class="modal-dialog modal-lg" style="margin-top:20%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Uzdot Jautājumu</h4>
            </div>
            <div class="modal-body">
                <p id="demo" >trlololo</p>
                <button onclick="myFunction()">Click me</button>
                <select name="faqcategory" id='inputSelect' class="form-control form-group-margin input-lg">
                    <option value="" disabled selected>-Izvēlies-</option>
                    <option value="visparigie" />Vispārīgie
                    <option value="serveris"/>Serveris
                    <option value="pakalpojumi"/>Pakalpojumi
                </select>
                <textarea placeholder="Tavs Jautājums" class="form-control form-group-margin input-lg" rows="6"></textarea>
                <div class="clearfix"><button class="btn btn-lg btn-block btn-flat btn-primary" onclick="faqsend()">Iesūtīt jautājumu</button></div>
            </div>
        </div><!-- / .modal-content -->
    </div><!-- / .modal-dialog -->

</div>
<!-- / Template -->

I tested, if i put trlololo

outside of MODAL trololo change to Hello World, if i put it inside nothing happens.

But I really want it to work, because i want to send error to modal, so when button is pressed user get results to MODAL. But can`t get it to change. Outside it dose.

I am now spending hours on one thing. innerHTML changes div or

javascript code:

function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}

Modal:

<!-- Template -->
<div id="faqask" class="modal fade" tabindex="-1" role="dialog" style="display: none;">
    <div class="modal-dialog modal-lg" style="margin-top:20%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Uzdot Jautājumu</h4>
            </div>
            <div class="modal-body">
                <p id="demo" >trlololo</p>
                <button onclick="myFunction()">Click me</button>
                <select name="faqcategory" id='inputSelect' class="form-control form-group-margin input-lg">
                    <option value="" disabled selected>-Izvēlies-</option>
                    <option value="visparigie" />Vispārīgie
                    <option value="serveris"/>Serveris
                    <option value="pakalpojumi"/>Pakalpojumi
                </select>
                <textarea placeholder="Tavs Jautājums" class="form-control form-group-margin input-lg" rows="6"></textarea>
                <div class="clearfix"><button class="btn btn-lg btn-block btn-flat btn-primary" onclick="faqsend()">Iesūtīt jautājumu</button></div>
            </div>
        </div><!-- / .modal-content -->
    </div><!-- / .modal-dialog -->

</div>
<!-- / Template -->

I tested, if i put trlololo

outside of MODAL trololo change to Hello World, if i put it inside nothing happens.

But I really want it to work, because i want to send error to modal, so when button is pressed user get results to MODAL. But can`t get it to change. Outside it dose.

Share Improve this question edited Sep 9, 2015 at 14:12 Ɛɔıs3 7,89911 gold badges53 silver badges83 bronze badges asked Sep 9, 2015 at 14:02 user3241944user3241944 251 silver badge7 bronze badges 2
  • "inside" mean where exactly ? – Nilesh Mistry Commented Sep 9, 2015 at 14:23
  • Bootstrap Modal, the modal code. If the <p id="demo" >trlololo</p> is anywhere outside modal code that i gave, it changes to Hello World. But if it`s inside modal code, nothing happens. – user3241944 Commented Sep 9, 2015 at 14:26
Add a ment  | 

1 Answer 1

Reset to default 3

Your code seems to work on jsfiddle. However, I had to change when the javascript runs to inside the body, likely after the page has loaded. You can, however, switch to jQuery since Bootstrap requires it to handle your clicking of the button for you.

$('#yourButtonID').click(function() {

  $('#demo').html('Hello World!');

});

JSFiddle

I added a button to show the modal.

If you don't want to change your code then I would suggest adding myFunction() to the end of the page just before the end of the <body> tag.

发布评论

评论列表(0)

  1. 暂无评论