0

i'm trying passing "this" from onclick event to a function and than to pass this to another function, its working on the first function, but "this" not passed to the next function called from the first function ?

this is my code, please advice ?

function hideProcessing(nextThis) {
  nextThis.parentNode.parentNode.style.display = "none";
  nextThis.parentNode.style.display = "block";
}

function showProcessing(param) {
  param.parentNode.parentNode.style.display = "block";
  param.parentNode.style.display = "none";
  var thisElement = this

  setTimeout('hideProcessing(thisElement)', 3000);
}
<div>
  <span id="overlap">show me</span>
  <div id="sendContainer" class="sendContainer">
    <button onclick="showProcessing(this);">send</button>
  </div>
</div>
phuzi
  • 8,111
  • 3
  • 24
  • 43
benjo
  • 301
  • 1
  • 4
  • 10
  • u have the this stored in param. you made a mistake assigning thisElement = this. also you should not call setTimeout with string, there should be a function as first parameter. in that function you will still have access to 'param' which is your this. – Marcin Malinowski Sep 07 '17 at 21:12
  • try this.name or id like that . https://stackoverflow.com/questions/4195970/what-does-this-mean – zod Sep 07 '17 at 21:14

3 Answers3

0

Try setting a timeout with a wrapper function, also this, in the scope of the function, is the window.

Try:

function hideProcessing(nextThis) {
    nextThis.parentNode.parentNode.style.display = "none";
    nextThis.parentNode.style.display = "block";
}

function showProcessing(param) {
    param.parentNode.parentNode.style.display = "block";
    param.parentNode.style.display = "none";

    setTimeout(function(){//wrapper anonymous function
        hideProcessing(param);
    }, 3000);
}

Read this (pun intended) to understand a little more about scope and how and when use this.

Cheloide
  • 762
  • 5
  • 21
0

function hideProcessing(nextThis){
console.log('nextThis', nextThis);
nextThis.parentNode.parentNode.style.display = "none";
nextThis.parentNode.style.display = "block";

}
 
function showProcessing(param){
param.parentNode.parentNode.style.display = "block";
param.parentNode.style.display = "none";

setTimeout(hideProcessing(param), 3000);
}
<div>
  <span id="overlap">show me</span>
    <div id="sendContainer" class="sendContainer"> 
      <button onclick="showProcessing(this);" >send</button>
    </div>
</div>
Pang
  • 8,605
  • 144
  • 77
  • 113
Vamshi Gudipati
  • 889
  • 1
  • 7
  • 21
0

first you should correct that 'this' in your onclick function to param Secondly call your setTimeout function as following:

setTimeout(function(){
  hideProcessing(thisElement)
}, 3000);

setTimeout function takes a function as an input not a string

DEMO: http://jsbin.com/wopeyosovu/2/edit?html,js,console,output

Ankush Malik
  • 150
  • 1
  • 11