I want to hide the buttons if there are less than 50 words and I can not do it. The hide function () does not hide them. why?

0
<script language="javascript">

$(document).ready(function(e) {
    $("#b_ocultar").hide("fast");

});

function mostrar(){
    $("#texto").show("slow");
    $("#b_mostrar").hide("fast");
    $("#b_ocultar").show("fast");

    }

function ocultar(){
    $("#texto").hide("slow");
    $("#b_ocultar").hide("fast");
    $("#b_mostrar").show("fast");

}
    function ocultacion(){


    $("#b_mostrar").hide("fast");
    $("#b_ocultar").hide("fast");

}




<div>
<?php
//------------------------para el texto que aparece y se esconde----------------------

$array=explode(" ",utf8_decode($fila3['descripcion']));//guardamos las palabras con separación de espación

//con un for mostramos las palabras que queremos
if(count($array)<50){//por si tiene memos de 50 palabras

    for($i=0;$i<count($array);$i++){
        echo $array[$i] . " ";//para espaciar las palabras

    }

?>

<script language='javascript'>ocultacion();</script>

<?php   

}else{//si tiene más de 50 palabras

    for($i=0;$i<50;$i++){
        echo $array[$i] . " ";
    }
echo "...";//para que lo muestre si tiene más de 50 palabras
}

?>
</div>
<div  id="texto" style="display:none">
<?php
//muestra el resto del texto dentro del div
for($i=50;$i<count($array);$i++){
    echo $array[$i] . " ";
    }

?>
</div>
<!--------------------fin del texto-------------------------------------------->

<br>
<!---------------------botones de mostrar más y mostrar menos texto------------------------->
<button onClick="mostrar()" id="b_mostrar" type="button" class="btn btn-success">Mostrar m&aacute;s...</button>
<button onClick="ocultar()" id="b_ocultar" type="button" class="btn btn-success">Mostrar menos...</button>
</br></br>
    
asked by Jesús 20.04.2018 в 09:18
source

1 answer

1

It may be for the following topics:

  • You have the <script> tag badly closed, I never see that at the beginning it is closed, I only see a <div> and then you put the php function.
  • <script language="javascript"> is too old and deprecated, the ideal is to use <script type="text/javascript"> which is the standard of HTML 4 .
  • When you call ocultacion(); the buttons are not yet rendered. It is best to call it once the rendering of the DOM is finished.
  • HTML:

    $(document).ready(function() {
      ocultacion();
    });
    
        
    answered by 20.04.2018 в 14:53