Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

389
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!