Problem if a function of a Javascript function

3
  

Good, I have a problem and it only works for me the first time I give it, that is, the first if (what I hide it), but when I give it back it does not show it and it stays hidden the elements

function funcion_ocultar_mostrar () {
    var visible = true;

    //ocultar
    if(visible === true){
        textarea.style.display = 'none';
        btn_enviar.style.display = 'none';
        btn_ocultar.innerHTML= 'Mostrar';



        console.log('ocultar');

    }

    //mostrar
    else if(visible === false){
        //textarea.style.display = 'none';
        textarea.style.display = 'block';
        btn_enviar.style.display = 'block';
        btn_ocultar.innerHTML= 'Ocultar';



        console.log('mostrar');
    }

    visible = !visible;
}
  

may also be that it is not doing well at the time of showing it again. I do not know, I'm not sure.

    
asked by josanangel 14.10.2018 в 16:26
source

1 answer

2

You can do it this way:

function funcion_ocultar_mostrar () {

    //ocultar
    if(textarea.style.display !== "none"){
        textarea.style.display = 'none';
        btn_enviar.style.display = 'none';
        btn_ocultar.innerHTML= 'Mostrar';



        console.log('ocultar');

    }

    //mostrar
    else if(textarea.style.display !== "block"){
        textarea.style.display = 'block';
        btn_enviar.style.display = 'block';
        btn_ocultar.innerHTML= 'Ocultar';



        console.log('mostrar');
    }
}
    
answered by 14.10.2018 / 16:37
source