-1

Need to hide the clicked div.

I tried with an example in jsfiddle. As i am new to js i want a better solution.

<div class="t1">1</div>
<div class="t2">2</div>
<div class="t3">3</div>
<div class="t4">4</div>
<div class="t5">5</div>

https://jsfiddle.net/5Lh3yfov/

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • where is the link ?? – A.T. Nov 08 '16 at 11:16
  • 1
    @raj, when you say *better solution*, it implies that you have tried something and you want better than that. – Rajesh Nov 08 '16 at 11:18
  • 2
    Please refer [How to add click on div](http://stackoverflow.com/questions/20772954/add-click-event-on-div-tag-using-javascript) and [How to execute a function after X seconds](http://stackoverflow.com/questions/8252638/executing-javascript-after-x-seconds) – Rajesh Nov 08 '16 at 11:21

3 Answers3

3

Pls find the working snippet added below

$('body').on('click','.t11',function(){
    $(this).hide();
    var that = $(this);
    setTimeout(function() {
        that.show();
    }, 1000);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="t1 t11">1</div>
<div class="t2 t11">2</div>
<div class="t3 t11">3</div>
<div class="t4 t11">4</div>
<div class="t5 t11">5</div>
Rajesh Grandhi
  • 378
  • 3
  • 13
0

do do something like,

function hideAndShow(element, timeout){
  element.style.display = "none";
  setTimeout(function(){
      element.style.display = "block";
    },timeout);
}

hideAndShow(document.getElementById("myDiv"), 10000)
div#myDiv{
  width:100px;
  background-color: #f00;
  height:100px;
  }
<div id="myDiv">
  <div>
A.T.
  • 24,694
  • 8
  • 47
  • 65
0

Use setTimeout() method for that.

// get all div and iterate over them 
// for newer browser you can use Array.from 
// to convert NodeList to array
[].slice.call(document.querySelectorAll('.t')).forEach(function(e) {
  // attach event listener to the element
  e.addEventListener('click', function() {
    // hide the element
    this.style.display = 'none';
    // set timer
    setTimeout(function() {
      // show the element
      this.style.display = 'block';
      // bind the this context or you can use `e` 
    }.bind(this), 10000);
  })
});
<div class="t">1</div>
<div class="t">2</div>
<div class="t">3</div>
<div class="t">4</div>
<div class="t">5</div>
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188