Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
Toggle Problems. Some array's is not toggling

Why array 2 is not toggling?

"-" toggled, "+" not toggled

toggled

php code:

$html = '';
    $array = array('1' => 'Airport', '2' => 'Restaurant', '3' => 'Market');

    foreach ($array as $key => $name) {
        $x = $key;
            $html .= '<table class="pad-3"><tr>';
            $html .= '<td><a href=\'javascript:toggle("' . $x . '");\'>
                        <img id="' . $x . '_img_1" src="../img/expandable_1.gif" width="9" height="9"/>
                        <img id="' . $x . '_img_2" src="../img/expandable_2.gif" width="9" height="9" style="display:none"/> ' . $name . '</a>
                     </td>';
            $html .= '</tr></table>';
            $html .= '<div id="' . $x . '_div_1" style="margin-left:15px;display:none;">';

            $html .= '</div>';
    }
    echo $html;

javascript:

function toggle(type) {
var a = $('#' + type + '_div_1');
var b = $('#' + type + '_img_1');
var c = $('#' + type + '_img_2');

if (a.is(':visible')) {
    a.hide();
    b.show();
    c.hide();
} else {
    a.show();
    b.hide();
    c.show();
}

}

Temporary Fix: I tried to change my Restaurant array index to 3. sample: "$array = array('1' => 'Airport', '3' => 'Restaurant', '4' => 'Market');"

Somehow the when I assign a index as 2 its not toggling. I'm having problems when I set the index to 2

Additional Info: when I add an alert line on the js. When I click the Restaurant the alert will prompt but still not toggling

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You REALLY need to delegate

I have simplified the whole thing - gotten rid of the needless table and the images

window.addEventListener("load",function() {
  document.getElementById("container").addEventListener("click",function(e) {
    const tgt = e.target.closest("a");
    if (tgt && tgt.classList.contains("toggle")) {
      e.preventDefault(); // stop link
      let isHidden = tgt.nextElementSibling.hidden; 
      tgt.nextElementSibling.hidden = !isHidden;
      tgt.querySelector("span.exp").classList.toggle("plus",!isHidden)
      tgt.querySelector("span.exp").classList.toggle("minus",isHidden)
    }
  })
})
.content {
  margin-left: 15px;
}

.pad-3 {
  padding: 3px
}

a.toggle {
  text-decoration: none
}

.exp {
  font-size: 12px;
  padding-right: 10px;
}

.exp.plus::after {
  content: "⊞"
}
.exp.minus::after {
  content: "⊟"
}
<div id="container">
  <a href="#" class="pad-3 toggle"><span class="exp plus"></span>Airport</a>
  <div class="content" hidden>Airport</div><br/>
  <a href="#" class="pad-3 toggle"><span class="exp plus"></span>Restaurant</a>
  <div class="content" hidden>Restaurant</div><br/>
  <a href="#" class="pad-3 toggle"><span class="exp plus"></span>Market</a>
  <div class="content" hidden>Market</div><br/>
</div>

PHP

<div id="container">
  <?php
    $array = array('1' => 'Airport', '2' => 'Restaurant', '3' => 'Market');
    foreach ($array as $key => $name) { ?>
    <a href="#" class="pad-3 toggle"><span class="exp plus"></span><?= $name ?></a>
    <div class="content" hidden>
      <?= $name ?>
    </div><br/>
    <? } ?>
</div>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda