简体   繁体   中英

Auto show a 'div' after hide another 'div' in javascript

I have a user question form and while I submit the form using ajax and saved to database, a successful message will show and after few second this message will disappear and the user question form will automatically show again.

I have able to disappear the successful message but not able to show the user question form again automatically.

Here is my html markup:

<div class="so-widget-sow-editor so-widget-sow-editor-base question-modal">

    <div class="siteorigin-widget-tinymce textwidget">
        <h3>Ask Question</h3>
        <form>
            <div class="form-group">
                <input type="text" id="question_form_name" class="form-control" id="exampleInputPassword1" placeholder="Your name">
                <div id="question_form_name_error_message" class="val_error"></div>
            </div>

            <div class="form-group">
                <textarea class="form-control" id="question_form_text_area" rows="3" placeholder="Ask your question" style="height:10em;"></textarea>
                <div id="question_form_text_area_error_message" class="val_error"></div>
            </div>

            <button type="button" class="btn btn-primary" id="question_form_button">Submit</button>

        </form>
    </div>
</div>

Here is my Javascript code:

$.ajax({

            url:"{{ url('/ask_question_form') }}",
            type: 'GET',
            data: {_token :token, name : name, email : email, contact : contact, question : question},
            success:function(msg){

                // console.log(msg);

                $('.question-modal .textwidget').hide();

                trHTML = "";
                trHTML += "<div id='user-question' style='margin-top:50%; color:#0071BC'>";

                trHTML += "Thanks for your question. We will save your question for further query and give a feedback as soon as possible.";


                trHTML += "</div>";

                $('.question-modal').append(trHTML);

                setTimeout(function() {
                    $('#user-question').hide();
                }, 10000);

                $('.question-modal .textwidget').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​//----this part is not working
            }
        });

Place the show again code inside the setTimeout() :

Change the following code

setTimeout(function() {
    $('#user-question').hide();
}, 10000);

$('.question-modal .textwidget').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​

To

setTimeout(function() {
    $('#user-question').hide();
    $('.question-modal .textwidget').show();​​​​​​
}, 10000);

Keep this line

$('.question-modal .textwidget').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​

in timeout

            setTimeout(function() {
                $('#user-question').hide();
                $('.question-modal .textwidget').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​//----this part is not working
            }, 10000);

$.ajax({

        url:"{{ url('/ask_question_form') }}",
        type: 'GET',
        data: {_token :token, name : name, email : email, contact : contact, question : question},
        success:function(msg){

            // console.log(msg);

            $('.question-modal .textwidget').hide();

            trHTML = "";
            trHTML += "<div id='user-question' style='margin-top:50%; color:#0071BC'>";

            trHTML += "Thanks for your question. We will save your question for further query and give a feedback as soon as possible.";


            trHTML += "</div>";

            $('.question-modal').append(trHTML);

            setTimeout(function() {
                $('#user-question').hide();
                $('.question-modal .textwidget').css('display'​​​​​​​​​​​​​​​​​​​​​​​​​​​,'block');​​​​​​//----this part is not working
            }, 10000);


        }
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM