Javascript variable loses its value

Popescu Ion

Background: I use butSubmit to submit the form which has the Dropzone and some other fields - the dropzone is processed first, and if everything goes right than the fields are processed too

Problem: The files (from dropzone) are uploaded correctly, and error3 gets the DropzoneOK value. However, when I access the value on button click, it is shown empty. I tried with var error3="" outside the function too (that means, on line 4).

<script>
  $(document).ready(function() {
    Dropzone.autoDiscover = false;

    var myDropzone = new Dropzone('#myDropzone', {       
      autoProcessQueue: false,
      uploadMultiple: true,
      parallelUploads: 20,
      maxFiles: 20,
      addRemoveLinks: true,
      init: function() {
        this.on("errormultiple", function(){
          error3="DropzoneErr";
        });
        this.on("queuecomplete", function () {
          this.removeAllFiles();
        });
        this.on("successmultiple", function(file, response) {
          error3="DropzoneOK";   //It works
        });
      }
    });

    $('#butSubmit').click(function () {
       var error3="";
       myDropzone.processQueue();
       console.log(error3);  //error3 is shown to be empty
       if(error3!="DropzoneOK")
            error3 = "<p style='color:red'>Please try again</p>";
    });

  });
</script>
Brett Jeffreson

You're declaring this variable again with an empty value.

$('#butSubmit').click(function () {
       var error3=""; <--- wrong, remove this
       myDropzone.processQueue();
       console.log(error3);  //error3 is shown to be empty
       if(error3!="DropzoneOK")
            error3 = "<p style='color:red'>Please try again</p>";
    });

Once you've removed that line add var error3="" as the very first line like so:

$(document).ready(function() {
    var error3="";
    Dropzone.autoDiscover = false;

    var myDropzone = new Dropzone('#myDropzone', {       
      autoProcessQueue: false,
...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Variable loses its value PHP

Variable loses value on if statement in javascript

The variable loses its value outside the function?

Global Variable loses its value after page index changed in DataGrid

Flutter variable loses value

Global variable loses its value after providing its value in a function.Unable to access its value outside that function

global variable loses its value when its called in main C++

Angular class variable loses value

Sorting variable loses value on pagination

"whois" output in a variable loses its formatting

Textbox loses its value on button click in jquery

.net core AsyncLocal loses its value

URL loses its markup in javascript framework (BotUI)

For loop loses variable value after first loop

Python variable loses value when passed to a if statement

Global variable loses value after userform

Constexpr variable captured inside lambda loses its constexpr-ness

JavaScript: Finding a variable's value by its name, is eval acceptable here?

Node Javascript - string variable setting to literal instead of its' value

What do we call the argument (variable or its value) of a function in Javascript?

Variable not retaining its value

Variable keeps its value

Session loses its data or value after form submitting in php

variable changing its value on its own in c

Static C++ variable, without default constructor, loses value

session variable loses value after detailsview new cancel event

Two forms one view: model variable loses value

unset a variable and then use its value

Use variable name, not its value