How to change the direction of the gradient?

There is a button clicking on which you want to change the direction of the background gradient in the block of #wrapper.
<button id="dir">&uarr;</button>
<div id="wrapper"></div>

I believe that the problem is variable scope. Help to fix it.
var dir = "to left";
$('#dir').click(function (){
 dir='to right';
function upgrade (id) {
 var startcolor = SF.grstartcolor.of the tohexstring() 
 var endcolor = SF.grendcolor.of the tohexstring() 
 document.getElementById('wrapper').style.background = 'linear-gradient(' + dir + ', ' + startcolor +', ' + endcolor + ')';
March 19th 20 at 08:47
1 answer
March 19th 20 at 08:49
You do not call upgrade, you simply change the value. The reinstallation of the gradient occurs.
Added another 2 lines which were in the code but don't understand what else you want to call. - Sean19 commented on March 19th 20 at 08:52
@Sean19, not, I not about them, they make the initial setting of the gradient. And should such a setup be called after each change dir. Just add a call to upgrade
$('#dir').click(function (){
 dir='to right'; // change the direction of the gradient
 upgrade(); // call the setup of the gradient with a new direction

Of course it is better not to use global variables, and pass the direction in the upgrade. - Irma commented on March 19th 20 at 08:55

Find more questions by tags JavaScript