Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript variable change with onclick

I want to have a variable changed within a checkbox that causes an if statement to compute without reloading the page, but I'm struggling... still learning, sorry!

Here's the relevant code:

 <!--Variable declaration-->
 <script type="text/javascript"> var lettuce = false; </script>

 <!--The following line is within a form-->
 <input type="checkbox" onclick="lettuce=true" name="food" value="lettuce" /> Lettuce<br />

 <--the if statement-->
 <script type="text/javascript">    
      if (lettuce == true) {
    document.write("45");   
  }
 </script>

Thanks!

like image 777
user868443 Avatar asked Jul 29 '11 19:07

user868443


People also ask

How do you change a variable in JavaScript?

Changing values In JavaScript, we can simply reassign the new value to the variable. When we change the value of a variable, we do not use var again. We simply use the = to give the variable a new value.

Can we use onclick in JavaScript?

The onclick event in JavaScript lets you as a programmer execute a function when an element is clicked.

Can I change a CSS variable with JavaScript?

CSS variables have access to the DOM, which means that you can change them with JavaScript.

What is JavaScript onclick?

The onclick event generally occurs when the user clicks on an element. It allows the programmer to execute a JavaScript's function when an element gets clicked. This event can be used for validating a form, warning messages and many more.


3 Answers

<input type="checkbox" onchange="lettuce=this.checked; recompute();" name="food" value="lettuce" /> Lettuce<br />

<script type="text/javascript">
function recompute()    
      if (lettuce == true) {
    document.write("45");   
  }
}
 </script>

First, you want the onchange event, which is called when the state of the checkbox changes. The this.checked returns the boolean value (true or false) depending on that state. Second, the if statement is only processed once, when the page loads that script. Place it in a function to call it again.

like image 196
Evan Mulawski Avatar answered Oct 18 '22 10:10

Evan Mulawski


First, your logic will never set lettuce to false. I understand that we should eat more lettuce, but we should still give the user a choice ;)

Secondly, try to keep your Javascript out of the html:

 <!--Variable declaration-->
 <script type="text/javascript"> var lettuce = false; </script>

 <!--The following line is within a form-->
 <input type="checkbox" id="lettuce" name="food" value="lettuce" /> Lettuce<br />

 <script type="text/javascript">  
      document.getElementById('lettuce').addEventListener('click',checkLettuce,false);
      function checkLettuce(){ 

          if (document.getElementById('lettuce').checked === true) {
             lettuce = true;
             document.write("45");  
          } 
          else
          {
             lettuce = false;
          }
      }
 </script>

Working demo: http://jsfiddle.net/AlienWebguy/renut/

like image 3
AlienWebguy Avatar answered Oct 18 '22 11:10

AlienWebguy


Wrap the if statement into a function and then call the function in the onclick event.

 <!--Variable declaration-->
 <script type="text/javascript"> var lettuce = false; </script>

 <!--The following line is within a form-->
 <input type="checkbox" onclick="my_function();" name="food" value="lettuce" /> Lettuce<br />

 <!--the if statement-->
 <script type="text/javascript">
      function my_function() {
          if (lettuce == true) {
              lettuce = false;
          } else {
              lettuce = true;
              document.write("45");
          }
      }
 </script>
like image 2
tskuzzy Avatar answered Oct 18 '22 12:10

tskuzzy