Search code examples
javascripthtmlcssdjangocodepen

Uncaught ReferenceError: $ is not defined problem


I've tried pasting the code copied from the code pen site below in the vault, but it doesn't work. I wonder why it's not working. What do I have to do to make it work? The CSS source code was imported using Viw compiled. Does this matter?

Code Pen Address https://codepen.io/dodozhang21/pen/vNOmrv

Error :

249 Uncaught ReferenceError: $ is not defined

It says this is the problem.

var $form = $("#imageUploadForm"),

Paste Source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    @import url(https://fonts.googleapis.com/css?family=Nunito);
@import url(//maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css);
*,
*:before,
*:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

html {
  height: 100%;
}

body {
  background: #ffd16e;
  height: 100%;
  padding: 0;
  margin: 0;
  font-size: 16px;
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: center;
  align-items: center;
  -webkit-justify-content: center;
  justify-content: center;
}

.uploadWrapper {
  font-family: "Nunito", sans-serif;
}

.imageUploadForm {
  background: #6e95f7;
  height: 400px;
  width: 500px;
  position: relative;
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: flex-end;
  align-items: flex-end;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-flex-wrap: wrap;
  flex-wrap: wrap;
}
.imageUploadForm .helpText {
  color: white;
  display: block;
  position: absolute;
  top: 2%;
  left: 0;
  width: 100%;
  height: 100%;
  text-align: center;
  font-size: 30px;
}
.imageUploadForm .helpText:after {
  content: "\f067";
  font-family: "FontAwesome";
  font-size: 150%;
  color: rgba(255, 255, 255, 0.5);
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: center;
  align-items: center;
  -webkit-justify-content: center;
  justify-content: center;
  margin: 4% auto auto auto;
  width: 50%;
  height: 50%;
  border: 6px dashed rgba(255, 255, 255, 0.5);
}
.imageUploadForm .pickFile {
  position: absolute;
  bottom: 0;
  left: 0;
  display: block;
  background: white;
  height: 25%;
  width: 100%;
  display: -webkit-flex;
  display: flex;
  -webkit-align-items: center;
  align-items: center;
  -webkit-justify-content: center;
  justify-content: center;
}
.imageUploadForm .pickFileButton {
  display: inline-block;
  padding: 0.7em 2em;
  color: white;
  background: #fb92ae;
  text-decoration: none;
}
.imageUploadForm .pickFileButton:hover {
  text-decoration: none;
}
.imageUploadForm .uploadButton {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
  z-index: 10;
}

.imageUploadForm.loading .helpText {
  font-size: 0;
  top: 7%;
}
.imageUploadForm.loading .helpText:before {
  font-size: 30px;
  content: "Uploading...";
}
.imageUploadForm.loading .helpText:after {
  display: none;
}
.imageUploadForm.loading .uploadedImg {
  position: absolute;
  bottom: 12.5%;
  left: 12.5%;
  width: 75%;
  height: 65%;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  border: 4px solid white;
  -moz-transition: opacity ease-out;
  -o-transition: opacity ease-out;
  -webkit-transition: opacity ease-out;
  transition: opacity ease-out;
}
.imageUploadForm.loading .unveil {
  position: absolute;
  background: #6e95f7;
  display: block;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  -moz-animation: toTop 4s forwards;
  -webkit-animation: toTop 4s forwards;
  animation: toTop 4s forwards;
}
.imageUploadForm.loading .unveil:after {
  content: "";
  position: absolute;
  left: -3%;
  bottom: 0;
  height: 10px;
  width: 106%;
  background: #ffd16e;
  border-radius: 5px;
}
.imageUploadForm.loading .pickFile,
.imageUploadForm.loading .uploadButton {
  display: none;
}

.imageUploadForm.loading.loaded {
  height: 200px;
}
.imageUploadForm.loading.loaded .uploadedImg {
  opacity: 0;
}
.imageUploadForm.loading.loaded .helpText:before {
  content: "Upload Complete!";
}
.imageUploadForm.loading.loaded .helpText:after {
  display: block;
  opacity: 0;
  font-size: 0;
  line-height: 100px;
  -moz-animation: fadeIn 0.4s forwards;
  -webkit-animation: fadeIn 0.4s forwards;
  animation: fadeIn 0.4s forwards;
  content: "\f058";
  color: white;
  margin-top: 2%;
  border-width: 0;
}

@-moz-keyframes toTop {
  to {
    height: 0;
  }
}
@-webkit-keyframes toTop {
  to {
    height: 0;
  }
}
@keyframes toTop {
  to {
    height: 0;
  }
}
@-moz-keyframes fadeIn {
  to {
    opacity: 1;
    font-size: 90px;
  }
}
@-webkit-keyframes fadeIn {
  to {
    opacity: 1;
    font-size: 90px;
  }
}
@keyframes fadeIn {
  to {
    opacity: 1;
    font-size: 90px;
  }
}

</style>

<body>
<div class="uploadWrapper">
  <form id="imageUploadForm" class="imageUploadForm">
    <span class="helpText" id="helpText">Upload an image</span>
    <input type='file' id="file" class="uploadButton" accept="image/*" />
    <div id="uploadedImg" class="uploadedImg">
      <span class="unveil"></span>
    </div>
    <span class="pickFile">
      <a href="#" class="pickFileButton">Pick file</a>
    </span>
      <script>
          function readURL(input) {
  if (input.files && input.files[0]) {
    var reader = new FileReader();

    reader.onload = function (e) {
      $uploadedImg[0].style.backgroundImage = "url(" + e.target.result + ")";
    };

    reader.readAsDataURL(input.files[0]);
  }
}

var $form = $("#imageUploadForm"),
  $file = $("#file"),
  $uploadedImg = $("#uploadedImg"),
  $helpText = $("#helpText");
$file.on("change", function () {
  readURL(this);
  $form.addClass("loading");
});
$uploadedImg.on(
  "webkitAnimationEnd MSAnimationEnd oAnimationEnd animationend",
  function () {
    $form.addClass("loaded");
  }
);
$helpText.on(
  "webkitAnimationEnd MSAnimationEnd oAnimationEnd animationend",
  function () {
    setTimeout(function () {
      $file.val("");
      $form.removeClass("loading").removeClass("loaded");
    }, 5000);
  }
);

      </script>
  </form>
</div>
</body>
</html>

Solution

  • You have to import jquery in your HTML file, do this before you import your own scripts. This is one source to import jquery: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    Here you can find more info about jquery: https://www.w3schools.com/jquery/jquery_get_started.asp