"What might be causing the error 'cannot access property 'top' of undefined' while trying to read

My website has a sticky navbar fixed at the top, and this is the structure of my sticky navbar:

$(function() {
  $(window).scroll(function() {
    if ($(window).scrollTop() > $(".b").offset().top + $(".b").height() && $("input").val() == "") {
      $(".sticky").show();
    } else {
      $(".sticky").hide();
    }
  });
});
.container {
  width: 1020px;
  margin: 0 auto;
}
.container>div {
  width: 100%;
  height: 300px;
  background: #f0f0f0;
  border: 1px solid #ccc;
  margin: 100px 0;
}
.a:after {
  content: "A";
  font-size: 250px;
  text-align: center;
  line-height: 300px;
  display: block;
  color: #999;
}
.b:after {
  content: "B";
  font-size: 250px;
  text-align: center;
  line-height: 300px;
  display: block;
  color: #999;
}
.c:after {
  content: "C";
  font-size: 250px;
  text-align: center;
  line-height: 300px;
  display: block;
  color: #999;
}
ul.sticky {
  list-style-type: none;
  padding: 0;
  margin: 0;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  background: #f0f0f0;
  height: 50px;
  border-bottom: 5px solid #ccc;
  display: none;
}
ul.sticky:after,
ul.sticky:before {
  content: "";
  display: table;
  clear: both;
}
ul.sticky li a {
  display: block;
  float: left;
  line-height: 50px;
  padding: 0 30px;
  text-decoration: none;
  color: #999;
}
ul.sticky li a:hover {
  background: #999;
  color: #f0f0f0;
}
<ul class="sticky">
  <li><a href="#">Home</a>
  </li>
  <li><a href="#">About Us</a>
  </li>
  <li><a href="#">Download</a>
  </li>
  <li><a href="#">Forums</a>
  </li>
  <li><a href="#">Contact</a>
  </li>
</ul>

<div class="container">
  <input type="text" class="input">
  <div class="a"></div>
  <div class="b"></div>
  <div class="c"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>

Click here to view on CodePen

In case I don't include the .sticky element on other pages, I receive an error from the JavaScript notifier. Is there a way to avoid adding the .sticky element on every page?

https://i.sstatic.net/uzwRR.jpg

View the real demo here

Click here to see the error message

Answer №1

This error occurs because jQuery was unable to locate the element .hotel-search-box on your website.

Javascript

$(function() {
  $(window).scroll(function() {
  if (!$(".hotel-search-box").length) {
     return false; //Ensure the element exists
  }
  if($(window).scrollTop() > $(".hotel-search-box").offset().top+$(".hotel-search-box").height() && $(".oda-giris-cikis").val() == ""){
          $(".sticky-checkin").show();
      }else{
          $(".sticky-checkin").hide();
      }
  });
});

To resolve this issue, include a .hotel-search-box element in your webpage where you want the sticky menu to appear.

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

Eliminating "www" from the domain name leads to a separate website

I am new to this. The lack of support from mediaTemple and my current hosting provider has been frustrating, so I am turning to stackOverflow for help. My issue is that entering "www" before the domain name or leaving it out leads to different servers. ...

What is the best way to combine a string with a variable in sass?

Is there a way to merge a string and a variable in Sass to form a variable name that is already present? $size: "sm"; $button-sm: 1rem; $buttom-md: 1.5rem; body { font-size: $button-#{$size}; } The desired output is: body { font-size: 1r ...

Using cakePHP to submit a form using ajax

While submitting a form using ajax and attempting to return a json response, I encountered an issue of receiving a missing view error. Adding autoResponder=false resulted in no response at all. This is happening while working with cakephp 2.5 In the same ...

Having trouble with Socket.io and its io.emit() method refusing to work? If communication from server to client isn't going smoothly, you may need a solution for sending data from the server to

My latest project is a document converter program that utilizes LibreOffice to convert documents to PDF format. Here's my server running on localhost:3000 import express from "express"; import bodyParser from "body-parser"; import ...

"Trouble arises with the match() function in relation to email regex validation

After retrieving the HTML content from a website with my function, I am using String.prototype.match along with a regex rule to extract email addresses from that page. However, the issue is that I am receiving a line that matches the regex but does not con ...

The response parser in Angular 7 is failing to function correctly

Hey, I recently updated my Angular from version 4.4 to the latest 7 and after encountering several errors, I was able to get my service up and running. However, I'm facing an issue with my output parser function which is supposed to parse the login re ...

The dropdown menu disappears when I hover over the tab, making it impossible for me to navigate it in the react app

My navigation component includes a Games tab with a dropdown menu that appears when you hover over it. However, I'm facing an issue where the dropdown menu closes before the user can transition from hovering over the games tab to the actual dropdown m ...

Highcharts: Including plotlines in the legend

I need to include a plotline in the legend. Check out my example here. $('#container').highcharts({ xAxis: { tickInterval: 24 * 3600 * 1000, // one day type: 'datetime' }, yAxis: { plotLines: ...

Steer clear of Cross-Site Request Forgery through

As someone who is still learning about web security, I am curious about the best practices for using tokens on JavaScript requests to prevent CSRF attacks. Is it possible for someone to provide a code example? I already know how to implement this properly ...

Utilizing jQuery to dynamically add a class based on specific criteria

I need help with the following scenario: <span class="dateStart" val="0">2016-09-16</span> | <span class="dateStart" val="1">2016-09-15</span> | Can anyone guide me on how to use jQuery to add a selected class to the <span> ...

The React form is only transmitting a single property from the state instead of the complete state

My current challenge involves sending form data from my react client to my nodejs server. The issue I am facing is that only the last property of the state gets sent to the server upon form submission. This problem seems to be occurring on the client side, ...

Incorporating an Angular 2 Directive within the body tag of an

My goal is to create a custom directive that can dynamically add or remove a class from the body element in HTML. The directive needs to be controlled by a service, as I want to manage the visibility of the class from various components. Question: How ca ...

Identify the index of a list item using a custom list created from buttons

When dealing with a dynamically built list like this: <ul id="shortcuts"> <li><input type="checkbox" value="false"/><button>foo</button><button>-</button></li> <li><input type="checkbox" value ...

Having trouble with my CSS hover not functioning properly

I need some help with my code. Can you please take a look and let me know why the hover effect is not working? Thank you! <style> #moreDiscussHome:hover{ background-color: #ffffff; } </style> <a id="moreDiscussHome" style="co ...

Encountering issues due to overriding JSON.stringify

Recently, I developed a customized function for JSON.stringify implementation as shown below: JSON.stringify = function (item, replacer, space) { return JSON.stringify(item, replacer, space); } This modification led to multiple errors in AngularJS w ...

React: Updating a state before calling another function - Best practices

In my code, there is a state variable named list that gets updated whenever the function setList is called. The setting of the list happens within the function AddToList, where a new value is added to the existing values in the list. However, I have notice ...

Does the content from an AJAX request get loaded if you flush it using ob_flush()?

Imagine this scenario, where we are making an AJAX request and inserting the result inside a div with the id of "result". On the backend, the script is using ob_flush() to send the header but not terminating the request until it's explicitly terminat ...

Utilizing jQuery to accentuate a specific div element when it is positioned directly in the center of the browser window

I have multiple divs with id="scroll_1", "scroll_2", "scroll_3", and so on. I am trying to implement a feature where when any of these divs is in the center of the window, I want to change the background color using jQuery highlight. Currently, I am able t ...

The file reading code in my Node.js application suddenly stopped working

I have a basic web application that I am currently developing using Node.js and Express. This is a breakdown of my package structure: https://i.stack.imgur.com/D7hJx.png The entries in my questions.json file are outlined below: [ { "question": "Wh ...

Jquery(Ajax) call encountered a problem when attempting to send the object

Struggling to send data to a MongoLab DB using JS and JQueryAjax call. The issue lies in being able to see the OID in the DB but no data is actually populated. Upon checking the code and network settings, it's evident that the payload always remains e ...