Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
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 Respuestas
    Responde la pregunta

    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 Denunciar

    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 Denunciar
    Responde la pregunta
    Encuentra empleos remotos

    ¡Descubre la nueva forma de encontrar empleo!

    Top de empleos
    Top categorías de empleo
    Empresas
    Publicar vacante Precios Comercial
    Legal
    Términos y condiciones Política de privacidad
    © 2026 PeakU Inc. All Rights Reserved.
    Andres GPT
    Recomiéndame algunas ofertas
    Necesito ayuda