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

398
Visualizações
Show and hide div with ajax content

I have this code:

<div id="product_<?php echo $product->id; ?>"></div>

This is ajax js code:

$(function () {
    $('.expand').on('click', function (e) {
        e.preventDefault();

        var token = "<?php echo $this->security->get_csrf_token_name(); ?>";
        var hash = "<?php echo $this->security->get_csrf_hash(); ?>";
        var productID = $(this).data("id");

        $.ajax({
            type: 'GET',
            dataType: 'html',
            url: 'marketplaces/marketplaces/test_expand',
            data: {
                product_id: productID,
                token: hash
            },
            beforeSend: function () {

            },
            success: function (data, textStatus) {

                $("#product_" + productID).html(data);

            },
            error: function (xhr, textStatus, errorThrown) {
                alert('request failed');
            },
            complete: function () {},
        });
    });
});  

And this button:

<div data-id="<?php echo $product->id; ?>" class="expand">
    <a href="#" class="bt btn-primary btn-sm">Offers
        <i class="fas fa-chevron-down"></i>
    </a>
</div>

When I click button div with id=product_(ID) it show me data from ajax. It's working! I want to hide this div when I click again on button!

How is possible? Thank you!

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

0

$('[data-id=product_ID]').hide();

I think you can use like this code. Hope to be helpful to you.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can put condition with custom code for show hide functionality.

Here is the sample code:

           $(function () {
        
        $('.expand').on('click', function (e) {
            
            e.preventDefault();
            var currBoxObj = this;
            var token="<?php echo $this->security->get_csrf_token_name(); ?>";
            var hash="<?php echo $this->security->get_csrf_hash(); ?>";
            
            var productID = $(this).data("id");
            if(!$(currBoxObj).hasClass("showingblock")){
            
                    $.ajax({
                        type: 'GET',
                        dataType: 'html',
                        url: 'marketplaces/marketplaces/test_expand',
                        data: 
                    {
                        product_id: productID,
                        token: hash             
                    },
                        beforeSend: function () {
                            
                        },
                        success: function (data, textStatus) {
                            $(currBoxObj).addClass("showingblock");
                            $("#product_"+productID).show();
                            $("#product_"+productID).html(data);
                
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            alert('request failed');
                        },
                        complete: function () {
                        },
                    });
            }else{
                $("#product_"+productID).hide();
                $(currBoxObj).removeClass("showingblock");
            }
        });
    });
about 4 years ago · Juan Pablo Isaza Relatório

0

You should add a class to the element when you open it

$("#product_" + productID).addClass('is-open')

Then check if it has the class on the second click (above the ajax call)

if ($("#product_" + productID).hasClass('is-open')) {
  $("#product_" + productID).hide().removeClass('is-open')
  return
}

If you want to close all before opening one then its trivial to do:

$('.is-open').hide().removeClass('is-open')

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