Search code examples
javascriptjqueryslidedownslideup

How Initialize divs text containers with SlideUp JQuery Function


I have the next question: In this moment I'm developing a web application, and I'm configuring the forms of my page. However, they are really long, so I decided to create differents divs to implement the JQuery Function: slideUp(); , slideDown(); Nonetheless, every time I reaload my page every div is slidedown by default. How can I change this? I want every div be SlideUp at the beggining.

This is my JQuery Code:

    $(document).ready(function(){
    var control = 0;
    $(".subtitulo").click(function(){
        var contenido = $(this).attr('data-contenido');
        if(control==0){
            $(".contenidoAcordeon-" + contenido).slideUp("fast");
            control = 1;
        }else{
            $(".contenidoAcordeon-" + contenido).slideDown("fast");
            control = 0;
        }
    });
});

Thanks :) !


Solution

  • Well I can't see your form's html so I'm going to make some assumptions.

    You're javascript code only fires when .subtitulo is clicked. You could initialize this by using the following to trigger a click event when the script is loaded.

    $('.subtitulo').trigger('click');
    

    But it would be better to hide all but the first div (the one you want shown by default) with css.

    [class^=contenidoAcordeon-] {
        display: none;
    }
    
    [data-contenido=1] { // Guessing you can select your first div this way.
        display: block;
    }