1

I'm trying to append text within an HTML file, but only when is_sum_1 == true. I want the user to select something from a dropdown box before the conditional text gets appended, but the if statement seems to only become satisfied when var is_sum_1 = true; is outside of the function.

JavaScript function:

function dropDownListener(input_id, output_id) {
   var dropDown = document.getElementById(input_id);
   var is_sum_1 = true;

   dropDown.onchange = function() {
      document.getElementById(output_id).innerHTML = dropDown.value;
   }

dropDownListener('sum_1', 'print_sum_1');

Printing output and conditional text in HTML:

<div id="summary">
   <span id="print_sum_1"></span>

   <script>
      if (is_sum_1) {
         $('#summary').append("I'm trying to append text if condition met")
      }
   </script>
</div>
  • 2
    `is_sum_1` is outside the scope of your ` – Liftoff May 05 '21 at 20:56
  • Does this answer your question? [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Liftoff May 05 '21 at 20:57
  • there is no dropdown in HTML, you mean a Select or a detail element? – Mister Jojo May 05 '21 at 20:58

0 Answers0