why this code works here:

button.onclick = function() { block.classList.toggle('active'); if (this.innerHTML === 'скрыть') { this.innerHTML = 'подробнее'; } else { this.innerHTML = 'скрыть'; } } 

but this is no longer:

 function innerButton() { if (this.innerHTML === 'скрыть') { this.innerHTML = 'подробнее'; } else { this.innerHTML = 'скрыть'; } } button.onclick = function() { block.classList.toggle('active'); innerButton(); } 

What am I doing wrong and how to make it work?

I have a lot of buttons on the page and I want to make a function to change the name of them separately

  • innerButton(); -> innerButton.apply(this); , as an option other than the one proposed in the answer. - Rostyslav Kuzmovych

1 answer 1

so try

 function innerButton(element) { if ( element.innerHTML === 'скрыть' ) { element.innerHTML = 'подробнее'; } else { element.innerHTML = 'скрыть'; } } button.onclick = function(){ block.classList.toggle('active'); innerButton(this); } 

most likely this inside innerButton is innerButton

  • so also does not work does not work - Big D
  • unblocked the answer and - Sasuke
  • you are a genius =) damn it's cool =) please explain how it works inside? - Big D
  • zaguli about this in js - Sasuke