简体   繁体   中英

Show or Hide a div element using jQuery

I am new to jquery and I am trying to hide certain div element and then show them on success of my Ajax call. When the page loads, browser hides the div element, on Ajax success the element is shown, but again browser is hiding the div elements.

Code

 <script> $(document).ready(function() { $('#sidebar-container').hide(1000); $('#overall-status').hide(1000); $('#submit-date').click(function() { var processDate = $('#processDate').val(); alert(processDate); $.ajax({ type : "POST", url : "launchapptest", data : processDate, dataType : "json", success : function(result) { alert("Success"); $('#sidebar-container').css({ visibility : "visible" }); $('#overall-status').css({ visibility : "visible" }); } }); } ); }); </script> 

Please help me understand what is happening and how to avoid this.

Use jquery Show event.

$(document).ready(function() {

        $('#sidebar-container').hide(1000);
        $('#overall-status').hide(1000);


        $('#submit-date').click(function() {
            var processDate = $('#processDate').val();
            alert(processDate);
            $.ajax({
                type : "POST",
                url : "launchapptest",
                data : processDate,
                dataType : "json",
                success : function(result) {
                    alert("Success");
                    $('#sidebar-container').show();
                    $('#overall-status').show();

                }
            });
        }

        );

    });

The issue is resolved, I created a form submit button to initiate the Ajax call. changed it to normal input button. The page was reloading because of this.

I changed the submit button to input button, to resolve this issue.

Thanks a lot for all the help.

.hide() sets styling to display:none . You need to call .show() , instead of .css({visibility:'visible'});

You have to stop the current animation queue for each element first (as non-animated CSS changes will not add to that queue):

Also, as mentioned elsewhere show() is a better option to css visibility as hide sets display: none and not visibility .

  success : function(result) {
                alert("Success");
                $('#sidebar-container').stop().show();
                $('#overall-status').stop().show();
            }

Additionally you are possibily not stopping the form from submitting, so the page would reload and rehide the divs. Try stopping the default behavior of that button.

$('#submit-date').click(function(e) {
     e.preventdefault()

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