full rusian
full rusian

Reputation: 37

How to change a javascript boolean from a html button?

I have a button onClick in the HTML side. I'm using a javascript function to do some actions. I need to change the value of a boolean using the HTML button. I have been looking at the forum but so far I didn't find something. Somebody can guide me here?

Thanks.

code:

JS

var buttonClicked = false;

  window.addEventListener('load',function(){
  console.log(buttonClicked);
  document.getElementById('BtnGotit').addEventListener('onclick',function(){
    buttonClicked = true;
    console.log(buttonClicked);
  });
  });


 function desktopMove(e){

if(buttonClicked == true){
            var wH = $(window).height();
            var wW = $(window).width();
            var x = e.clientX;
            var y = e.clientY;
            if(x <= 20){
                //Left
                $pageTrigger = $('.pt-page-current').find('.right');
                    if($pageTrigger.length)
                        Animate($pageTrigger);
            }else if(x >= (wW - 50)){
                //Right
                $pageTrigger = $('.pt-page-current').find('.left');
                    if($pageTrigger.length)
                        Animate($pageTrigger);
            }else if(y <= 50){
                //Top
                $pageTrigger = $('.pt-page-current').find('.back');
                    if($pageTrigger.length)
                        Animate($pageTrigger);
            }else if(y >= (wH - 50)){
                //Bottom
                $pageTrigger = $('.pt-page-current').find('.next');
                    if($pageTrigger.length)
                        Animate($pageTrigger);
            }

        }
        }

HTML

> <button2 onclick="function()" class = "BtnGotit" >Ok, GOT IT.</button2><br>

Upvotes: 1

Views: 3851

Answers (3)

NVRM
NVRM

Reputation: 13047

On the simplest manner, this is a good start to toggle with one button.

<input id="Z" value="0" type="range" max="1">
<button onclick="(Z.value<1)?Z.value++:Z.value--">Switch</button>

Note that it is inline javascript, this isn't the best to do for complex programs, see instead the usage of EventListeners!

Upvotes: 0

Shaya Ulman
Shaya Ulman

Reputation: 1651

Here is a simple example how to approach it:

let bool = true

const changeValue = () => {
    bool = !bool
    console.log(bool)
}
<button onclick="changeValue()">Click Me!</button>

Upvotes: 1

www139
www139

Reputation: 5227

It's always a good idea to include your source code in the question because it helps us answer. You can add a code snippet with a demo of your code.

When the page loads, the button will be monitored for clicks. When clicked, the buttonClicked variable is set to true. buttonClicked value is initially false.

var buttonClicked = false;

window.addEventListener('load',function(){
  console.log(buttonClicked);
  document.getElementById('randoButton').addEventListener('click',function(){
    buttonClicked = true;
    console.log(buttonClicked);
  });
});
<input type="button" id="randoButton" value="Set the variable value">

Upvotes: 1

Related Questions