Hide a div when showing another

Asked

Viewed 966 times

4

I’m having a problem hiding one div when I show another. If I use .conteudo_up instead of the id2 I can do this effect, but it does for everyone at the same time and I want for each one individually.

How do I do that? http://codepen.io/Ryuh/pen/ezNqXJ

HTML

<div class="div_grande">
  <?php
    if (!mysqli_set_charset($conn, "utf8")) {
        printf("Error loading character set utf8: %s\n", mysqli_error($link));
        exit();
        } else {
        $sql = "SELECT idr, nome, video, img1, img2, img3, img4, img5, img6, telefone, morada, descricao, site FROM restaurantes WHERE fk_lingua = $idl AND fk_distrito = $idd ORDER BY idr DESC";
    }
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $id = $row['idr'];
        $strid = (string)$id;
        $id2 = '_'.$strid."cima";
?>
    <div class="conteudo_cima" id="<?php echo eval('return $'. $id2 . ';');?>">
      <iframe class="img_cima" src="<?php echo $row['video']; ?>" allowfullscreen=1 frameBorder=0></iframe>
      <div class="texto">
        <h1><?php echo $row['nome']."<br>";?></h1>
        <?php echo $row['descricao']."<br>";?>
        <?php echo $row['morada']."<br>";?>
        <?php echo $row['telefone']."<br>";?>
        <a href="http://<?php echo $row['site'];?>" target="_blank">
          <?php echo $row['site'];?>
        </a>
      </div>
      <div class="seta">
        <img id="trigger" src="imagens/expand.png" onclick="abreInfo(event, <?php echo $id; ?>)">
      </div>
    </div>
    <div class="expand" id="<?php echo $id; ?>">
      <div class="video">
        <iframe src="<?php echo $row['video']; ?>" allowfullscreen=1 frameBorder=0></iframe>
      </div>
      <div class="galeria">
        <div><img src="<?php echo $row['img1'] ?>"></div>
        <div><img src="<?php echo $row['img2'] ?>"></div>
        <div><img src="<?php echo $row['img3'] ?>"></div>
        <div><img src="<?php echo $row['img4'] ?>"></div>
        <div><img src="<?php echo $row['img5'] ?>"></div>
        <div><img src="<?php echo $row['img6'] ?>"></div>
      </div>
      <div class="fundo_expand">
        <div class="texto_expand">
          <h1><?php echo $row['nome']."<br>";?></h1>
          <?php echo $row['descricao']."<br>";?>
          <?php echo $row['morada']."<br>";?>
          <?php echo $row['telefone']."<br>";?>
          <a href="http://<?php echo $row['site'];?>" target="_blank"><?php echo $row['site']."<br><br>";?></a>
        </div>
        <div class="seta_expand">
          <img id="trigger_expand" src="imagens/encolher.png" onclick="abreInfo(event, <?php echo $id; ?>)">
        </div>
      </div>
    </div>
    <?php }
       } else {
          echo "Sem resultados disponíveis!";
       }
    ?>
</div>

JS

function abreInfo(event, id) {
  event.preventDefault();
  $("#" + id).toggle("slow");
}
$(document).ready(function() {
  $("#trigger").click(function() {
    $("#" + id2).css("display", "none");
  });
  $("#trigger_expand").click(function() {
    $("#" + id2).css("display", "block");
  });
});
  • 1

    You can put the relevant part of the code. HTML + JS/JQUERY? in codepen you can’t see

  • Updated. Thanks in advance for the help

  • Possible duplicate of Hide div by clicking another

  • That one id2 is what?

  • This is the variable I used to assign the database id to the content above, because id was assigned to div expand

  • php in the middle of html because?

  • to retrieve the information from the database

  • this answer already exists on the site, just search with a little attention.

  • it would be good to comment on the parts of the code with useful details, so we could have a better understanding of your code

  • @user2964140 the page is in php, but the question refers to the html part using css and js

  • @Joãorodrigocastro How do I do this? http://codepen.io/Ryuh/pen/ezNqXJ, link is broken

Show 6 more comments

2 answers

2

You can do it like this:

<div class="conteudo_cima" id="<?php echo $id2; ?>">
<img id="trigger_expand" data-show="<?php echo $id2; ?>" src="imagens/encolher.png">

Then it gets like this:

$(".trigger").click(function() {
  $('.conteudo_cima').hide();
});
$(".trigger_expand").click(function() {
  var divShow = $(this).data('show'); // id_da_bd
  $('.conteudo_cima').hide();
  $('#' +divShow).show(); // id_da_bd aparece
});
.conteudo_cima {
  display:none;  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="trigger">trigger1</div>
<div class="trigger_expand" data-show="id_da_bd">trigger expand1</div>

<div class="conteudo_cima" id="id_da_bd">caixa 1</div>

In case you need the $id2 for other things, no js does:

var id2 = $('.conteudo_cima').prop('id');
  • Yeah, that I got to do, what I can’t do is get the div that has Rigger to disappear and reappear when I press Rigger expand

  • I edited @Joãorodrigocastro

  • That’s not it. I think I just need to pass the value of id2 to js. How can I do it? Thanks for the help

  • I think I got it. I edited upstairs how you have to do it to print $id2 in html, and then how you get it in js

-1

I think the problem is that you do not initialize the id2 variable in javascript.

while($row = $result->fetch_assoc()) {
        $id = $row['idr'];
        $strid = (string)$id;
        $id2 = '_'.$strid."cima";
?>
<script>
    var id2 = <?php echo $id2 ?>
</script>

And change the click from . click() to

$('body').on('click','#trigger',function(){
    $("#" + id2).css("display", "none");
})
  • It didn’t work. id2 in this case is only used to identify which div conteudo_cima I want to hide. I have several because I create one for each entry of the database

  • But is id2 (in javascript) initialized somewhere? It’s not noticeable in the code you sent. The Programmer Tool did not fail?

  • Because id2 is not initialized in js. How can I pass the value of php $id2 in js?

  • I do not advise much the structuring of the code, however to solve you can do as it is edited above

  • So do not step to my js function. I can use the $ no js?

  • I can’t test it now

  • That’s supposed to be it. It didn’t work, I tried to make a window.Alert() and noticed that it’s not entering .click. functions. I’ll try to use the onclick

  • Check up on what I edited

Show 3 more comments

Browser other questions tagged

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