JavaScript var is not defined

Hey there everyone! I'm facing an issue with this variable in my file. I have a picture, and it's supposed to display a swipe box with some information about the image when the user hovers over it. However, for some reason, the box is not appearing.

Here is the HTML/JS code:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
    <link href="css/custom.css" rel="stylesheet">
    <script type="text/javascript">
    var modal = $('.modal');

    modal.hover(function() {
        $(this).toggleClass('open');
    });

    alsolike(
      "qEybdR", "Checkbox Radial Wash",
      "dPdoNp", "Google Messenger Icon Button",
      "BJAjF", "Masonry Multi Column Grid"
    );
    </script>
</head>
<body>

<a class="drib" href="http://drbl.in/okRS">View it on Dribbble <i class="fa fa-dribbble"></i></a>
<div class="modal">
  <div class="image-container"></div>
  <div class="content">
    <div class="wrapper">
      <div class="category">First Drive Review</div>
      <h2>2015 Dodge Challenger SRT Hellcat</h2>
      <h4>Andy Wendler <span>from</span> July 2014</h4>
      <div class="line"></div>
      <p>Make room, Beelzebub, there’s a new demon-prince pony car in town, and it’s from the people who once brought you a real Demon. Known in this mortal realm as the Dodge Challenger SRT Hellcat...</p><a href="http://www.caranddriver.com/reviews/2015-dodge-challenger-srt-hellcat-first-drive-review">Read More <i class="fa fa-caret-right"></i></a>
    </div>
  </div>
</div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
</body>
</html>

And here is the CSS code:

.modal {
  width: 600px;
  height: 400px;
  margin: 50px auto;
  border-radius: 5px;
  overflow: hidden;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
  position: relative;
  -webkit-transition: all 0.5s;
  transition: all 0.5s;
}
.modal .image-container {
  background: url("http://media.caranddriver.com/images/14q3/612022/2015-dodge-challenger-srt-hellcat-first-drive-review-car-and-driver-photo-615298-s-original.jpg");
  background-size: cover;
  background-position: center center;
  display: inline-block;
  width: 100%;
  height: 100%;
  -webkit-transition: all 0.5s;
  transition: all 0.5s;
}
.modal .content {
  width: 40%;
 

Thank you for any assistance you can provide!

Answer №1

In order to utilize jQuery, it is necessary to include the jQuery library first. For example:

<script type="text/javascript">
var modal = $('.modal');
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

should be:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
var modal = $('.modal');
</script>

There is some discussion about the optimal placement of these scripts (in the <head> section for accessibility or just before </body> for faster content loading), but the key is to ensure they are in the correct sequence.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How come React.js is not displaying images from local URLs?

In my React application, there is a form where users can submit an image file. To store the path of the submitted image locally for browser access, I utilized URL.createObjectURL to generate a URL for the file. Here is the code snippet: handleImageChange(e ...

Utilizing BootStrap for a more Dynamic and Reactive Web Design

Here is the HTML code I am using along with bootstrap: <div class="row"> <div class="col-sm-4" style="background-color:lavender;">.col-sm-4</div> <div class="col-sm-8" style="background-color:lavenderblush;">.col-sm-8</div> & ...

Enabling Context Isolation in Electron.js and Next.js with Nextron: A Step-by-Step Guide

I've been working on a desktop application using Nextron (Electron.js + Next.js). Attempting to activate context isolation from BrowserWindow parameters, I have utilized the following code: contextIsolation: true However, it seems that this doesn&ap ...

Is it possible to scroll by using the dragenter event?

Looking for a way to achieve incremental scroll up and scroll down using jQuery without jQuery UI? Here's the scenario - I have two divs: <div class="upper" style="height:35px;background-color:red;right:0;left:0;top:0;position:fixed;width:100%;z-i ...

How to create a fixed header navigation in SquareSpace

If you take a look at my Squarespace website using the Adirondack theme over here: The issue I'm facing is that whenever I scroll on either desktop or phone, the top header area shrinks. Currently, the navigation remains fixed on the screen and the l ...

Rendering a React component conditionally within the same line

I'm trying to conditionally render a Home component based on a certain condition. I attempted to utilize the Inline If-Else with Conditional Operator recommended by React, as explained in this source. The code snippet I have looks like this: import ...

Guide on assigning a material to ColladaLoader or OBJLoader

I've searched extensively through the documentation and numerous examples, but I couldn't find the correct syntax for assigning a material to either a Collada .dae or OBJLoader .obj file. Json files seem to work well when creating a Mesh, with t ...

Is it possible to adjust the position of a h2 heading on a particular webpage using CSS?

There is a situation where multiple pages contain h elements nested inside a standard container div class. When trying to edit a specific h2 element in a specific page using CSS, simply editing the h2 or the container does not work. Other methods attempte ...

When using angular-ui-select and having an empty <option>, it displays two blank fields

I have implemented angular-ui-select for dropdowns and I want to include a cancel feature in the dropdown. This is the dropdown structure in my template: <select ui-select2="select2Options" name="Machine" ng-model="selectedMachine.type" data-placehold ...

What is the reason behind the NgForOf directive in Angular not supporting union types?

Within my component, I have defined a property array as follows: array: number[] | string[] = ['1', '2']; In the template, I am using ngFor to iterate over the elements of this array: <div *ngFor="let element of array"> ...

Refreshing jq grid after receiving JSON data to display dynamic columns

I am currently working on an asp.net mvc project where I have implemented a Jqgrid with dynamically generated columns. The challenge I am facing is reloading the grid with new data when a different date is selected. The issue I am encountering is that the ...

Vuejs Todolist List App experiences unanticipated alteration in task prop

I own a store that includes state, mutation, getters, and more. Within the state, there is a list of tasks like the ones shown below. const state = { tasks:[{ title: "Wake up", completed: false }, { title: "Item 2&quo ...

What is the best way to send an array of strings through an AJAX call?

Utilizing ajax to send an array of strings to another page has been a bit tricky for me. Here is the array located on nomes.php [ 'Home','A empresa','funcionarios','Quem Somos?','Historia','Inicio&ap ...

How to retrieve the optgroup label from a Bootstrap Select dropdown

Currently, I am working with a BootStrap select box and my goal is to identify the optgroup of the option that has been selected. The following code snippet showcases a bootstrap select box with different options: <li rel="1"> <div class="div- ...

What steps can I take to ensure my CSS component remains unaffected by the global CSS styles?

My navbar component is not displaying the styles correctly as intended. I have a Navbar.module.css file to style it, but after using next-auth for social login, only the buttons remain unstyled while everything else gets styled. The code snippet for impor ...

The Action Creator is not being waited for

In my application, I am using a placeholder JSON API to fetch posts and their corresponding users. However, I encountered an issue where the user IDs were being duplicated and fetched multiple times. To resolve this, I implemented the following code snippe ...

The jQuery ajax function is not properly displaying or hiding the loader div

While attempting to call PHP code using a jQuery AJAX function, I encountered an issue where I wanted to display a loader div while the request was being sent and then hide the div once the request was complete. To simulate this scenario, I deliberately de ...

Using React to display data from a nested JSON object in a table

I am currently working on parsing a JSON object into a table using React. However, I am facing an issue with utilizing the .map() function to create a row for every unique combination of course code, name, transferable_credits, transferable_credits -> i ...

The new and improved Vue 3 computed feature in the Composition API

The temporary object appears as: tmp : { k1: { k2 : { k3 : [abc, def] } } To access k3 in the setup, it should be: tmp.value.k1.k2.k3[0 or 1]. I am looking to change its name to something like - k3_arr = tmp.value.k1.k2.k3; Within my Vue single componen ...

Utilize CSS to create a visual buffer at the bottom of the header

I'm having trouble creating a gap between the bottom of "headermenu" and the top of "list". No matter what values I use for margin-bottom or padding-bottom, the table won't budge. I could try adding margin-top to the table, but I would prefer to ...