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

199
Views
How to display specific div onclick using js

I have multiple

  • with various div inside. On click of more or less a tag I want to display specific
  • i.e. display_on_click

    And I want to add that class only for 2 minutes after that remove that class and hide div

    My HTML code as below:

    $('body').on("click", ".more, .less", function() {
      var obj = $(this);
      obj.closest('.product_info').find('.display_on_click').addClass('display_on_click_show');
    });
    .display_on_click {
      display: none
    }
    
    .display_on_click.display_on_click_show {
      display: block;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div class="main">
      <ol class="item_wrapper">
        <li class="product_info">
          <div class="title">
            <h3>Title here</a>
          </div>
          <div class="incre_decre">
            <a class="more">+</a>
            <a class="less">+</a>
          </div>
          <div class="display_on_click">Updated</div>
        </li>
        <li class="product_info">
          <div class="title">
            <h3>Title here</a>
          </div>
          <div class="incre_decre">
            <a class="more">+</a>
            <a class="less">+</a>
          </div>
          <div class="display_on_click">Updated</div>
        </li>
        <li class="product_info">
          <div class="title">
            <h3>Title here</a>
          </div>
          <div class="incre_decre">
            <a class="more">+</a>
            <a class="less">+</a>
          </div>
          <div class="display_on_click">Updated</div>
        </li>
      </ol>
    </div>

    Anyone have idea what I am doing wrong then let me know. And I want to add that class only for 2 minutes after that remove that class and hide

  • about 4 years ago · Juan Pablo Isaza
    2 answers
    Answer question

    0

    I would do it like this:

    $('body').on("click", ".more, .less", function() {
      let parentLi = $(this).parent().parent();
    
      let elementToManipulate = obj.find('.display_on_click');
      elementToManipulate.addClass('display_on_click_show');
    
      setTimeout(()=> {
         elementToManipulate.removeClass('display_on_click_show')
      }, 120000);
    });
    

    Instead of using closests I'm grabbing the parent of the parent which is the <li>. You can use closest to grab it. Once I am in the parent li, I find the child with the .display... class (the elementToManipulate object).

    Then I add the classes and create a setTimeout to remove the class after 120.000 miliseconds (60sec * 2 * 1000msec)

    about 4 years ago · Juan Pablo Isaza Report

    0

    <button onClick='OpenDiv'>Open</button>
    
    function OpenDiv(){
        let div = document.getElementById('BoxOne')
        if (div.style.display == 'flex') {
            div.style.display = 'none';
        else
            div.style.display = 'flex';
    }
    

    I hope this will work 👍

    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!