Click on button and open list

Asked

Viewed 813 times

1

I’m trying to make the user, by clicking on the button, see all the contents of the database and when it is not on display 6 products (this is already ok), but the part of the button I’m breaking my head and I don’t know where I’m going wrong. Follows script:

This is the part of the code that displays 6 items (search in BD with a LIMIT 6):

<div class="col-md-9">

    <div class="row"> 

        <?php 
        $dao=new produtoDAO($conexao);
        $produtos = $dao->listaContainer();
        foreach($produtos as $produto) : 
        ?>

        <div class="col-md-4">
            <div class="card card-product card-plain">

                <div class="image">
                    <a href="precompra.php?id=<?=$produto->getId()?>">
                        <img src="../cadastro/fotos/<?=$produto->getFoto() ?>" alt="foto"/>
                    </a>
                </div>

                <div class="content">
                    <a href="#">
                        <h4 class="title"><?=$produto->getMarca()->getNome() ?></h4>
                    </a>
                    <p class="description">
                      <?=$produto->getDescricao() ?>
                    </p>
                    <div class="footer">
                        <span class="price"> R&dollar;<?=$produto->getPreco() ?></span>                     
                    </div>

                </div><!-- card-product -->

            </div> <!-- end card -->

        </div><!-- col-md-4 -->
        <?php endforeach?>

This is the part of the code referring to the Open Button more, I used None display, javascript function but could not make it work...

<script type="text/javascript">
$(function() {
$("#successBtn").click(function(){
if($("#produtos").css('display') === 'none'){
  $("#produtos").show();
}else{
  $("#produtos").hide();
}

});
});
</script>

                              <div class="col-md-3 col-md-offset-4" >                      
                              <button type="button" rel="tooltip" title="Abrindo..." class="btn btn-default btn-round" id="successBtn" data-toggle="morphing" data-rotation-color="red" >Mais Produtos...
                               </button>
                              </div>

                            <div class="col-md-4"  style="display:none;" id="produtos">

                            <?php 
                            $dao=new produtoDAO($conexao);
                            $produtos = $dao->listaProdutos();
                            foreach($produtos as $produto) : 
                              ?>
                            <div class="card card-product card-plain">

                                <div class="image">
                                    <a href="precompra.php?id=<?=$produto->getId()?>">
                                <img src="../cadastro/fotos/<?=$produto->getFoto() ?>" alt="foto"/>
                            </a>
                                </div>

                                <div class="content">
                                    <a href="#">
                                        <h4 class="title"><?=$produto->getMarca()->getNome() ?></h4>
                                    </a>
                                    <p class="description">
                                      <?=$produto->getDescricao() ?>
                                    </p>
                                    <div class="footer">
                                        <span class="price"> R&dollar;<?=$produto->getPreco() ?></span>                     
                                    </div>

                                <</div> <!-- end card -->




                         </div><!-- col-md-4 -->

                             <?php endforeach?>

                         </div>

                    </div><!-- row -->
                </div><!-- col-md-9 -->


           </div><!-- row -->
       </div><!-- container -->
</div><!-- section -->
  • I guess you just forgot the ID selector # in the click of successBtn - $("#successBtn").click(function () { ... }

  • Thanks Pedro, I changed and includes the selector but still nothing. The button is not working.

1 answer

0

You can try it this way and see if it resolves.

$(function() {
    $("#successBtn").click(function(){
        $("#produtos").fadeIn();
    });
});

Functional example.

$(function() {
  $("#successBtn").click(function(){
    if($("#produtos").css('display') === 'none'){
      $("#produtos").show();
    }else{
      $("#produtos").hide();
    }
    
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="button" value="mostrar produtos" id="successBtn">
<div id="produtos" style="display:none;">
<ul>
<li>Biscoito</li>
<li>Salgadinho</li>
<li>Refrigerante</li>
</ul>
</div>

  • Matthew made the change he suggested to me but it still didn’t work.

  • Try using jQuery’s show() and Hide(), show() to show, Hide() to hide: jQuery('#produtos').show();

  • hi Matthew changed and nothing. The button is in the condition "opening" and does not open the list of products. Where I am missing?

  • You can check if you enter the click function ?

  • jQuery.fn.offset = Function( options ) { if ( Arguments.length ) { Return options === Undefined ? this : this.each(Function( i ) { jQuery.offset.setOffset( this, options, i ); }); }

  • i found this offset function in jquery.1.10.2.js file would be this? or if not how I check if it is entering the click function?

  • You can debug with console.log() and check if it appeared on your browser console, example : $("#successBtn"). click(Function(){console.log('entered'); $("#products"). show(); });

  • Matthew does not appear on the island

  • I edited the answer and added a functional example, can be based on it.

  • Matthew, I made the change to my question as you directed me up there. The display: None is working (not listing) so it’s right to hide the listing, The button is not working yet, it’s running that Morphing. I believe the function is not running even indicating the buttonSuccess id. Our ta complicated even. You can help me find this error?

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.