Out of nowhere, Div became completely unresponsive

As part of my lesson plan on jQuery, I wanted to demonstrate a sliding effect using a div element. Initially, the sliding worked fine when clicked, but then I tried adding code for hiding another element and encountered issues. Even after removing the new code, the sliding functionality no longer works.

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
        // slide function
        $("#frog").click(function() {
            $("#frog").animate({left: "200px"}, 500);
        });

    </script>
    <style type="text/css">
        #frog{
            width: 100px;
            height: 100px;
            background-color: red;
            position: relative;
            left: 20px;
        }
    </style>
    <title>I hate JS!</title>
</head>

<body>
    <ul>
        <h2>Programming Languages</h2>
        <li>Python</li>
        <li>Javascript</li>
        <li>C++</li>
        <li>C#</li>
        <li>ruby</li>
    </ul>
    <ol>
        <h2>Top 3 Best Animations</h2>
        <h4><li>Slide</li></h4>

    <div id="frog"></div>
    </ol>

</body>

Answer №1

It seems like your HTML code is not valid, which can cause issues with how JavaScript functions as elements may not render correctly. To ensure proper functionality, your code should be structured like this:

<ul>
    <h2>Programming Languages</h2>
    <li>Python</li>
    <li>Javascript</li>
    <li>C++</li>
    <li>C#</li>
    <li>Ruby</li>
</ul>
<ol>
    <li><h2>Top 3 Best Animations</h2></li>
    <li>
      <h4>Slide</h4>
      <div id="frog"></div>
    </li>
</ol>
<script>//JavaScript code here</script>

Furthermore, it's recommended to place your JS before the closing body tag, ensuring that the script runs only after all elements are loaded. This will prevent any issues with finding elements before they are rendered. So remember to include scripts at the end of your document!.

Don't forget to encapsulate your JavaScript within:

$(function(){
 //Your JavaScript goes here
});

This ensures cross-browser compatibility and triggers the script when the page finishes loading (as not all browsers do this automatically).

For a functional example, check out this JSFiddle:

https://jsfiddle.net/5cxnLe4v/1/

-- And lastly, always remember to close each tag properly, including </body> and </html> tags!

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

AngularJs has the ability to parse dates effectively

So, I have this date: /Date(1451602800000)/. My goal is to display it in the 'short' format. Even after attempting {{myDate | date:'short'}}, the output remains as /Date(1451602800000)/ ...

Unending horizontal flow of divs inside a container div

I have been experimenting with creating a continuous strip of logos that display customer quotes when selected. I have successfully implemented functionality for selecting and displaying the logos and quotes. However, I am facing an issue with making the l ...

Repetitive Anchor IDs and Web Standards Compliance

Today, I encountered the task of converting outdated attributes on a page to make it more XHTML friendly. This led me to consider the anchor tag and its usage: Initially, I had anchor tags in the form of <a name="someName"></a>. However, upon ...

What is the process to obtain the child theme URL in WordPress?

Currently, I am working on creating a child theme for my WordPress website. I have successfully uploaded the assets folder which contains both CSS and JavaScript files. This child theme will be completely customized to suit my needs. Within the <head&g ...

Setting a background image in vanilla-extract/css is a straightforward process that can instantly enhance

I am brand new to using vanilla-extract/CSS and I have a rather straightforward question. I am attempting to apply a background image to the body of my HTML using vanilla-extract, but I am encountering issues as I keep getting a "failed to compile" error. ...

Vue.js is throwing an error because it cannot find the property or method "blah" when referencing it during rendering

Can you explain why Vue 2 throws an error indicating that a prop is not defined, even though it is statically defined in the parent template? Note: This error does not occur when I include the JavaScript code within the .vue file's script tag instead ...

Managing variables or properties that are not defined in ES6

Currently, I am utilizing Angular and Firebase Firestore in my project. However, the question posed could be relevant to Typescript and pure Javascript as well. The functionality of my application relies heavily on the data retrieved from Firestore. A sim ...

Exploring the integration of HTML5 in ASP.NET applications

Can you provide a sample demonstrating how I can implement offline caching using HTML code in my ASP.Net application? ...

Animation of disappearing blocks covering the entire screen

I am currently working on creating a slider with fading blocks animation similar to the one shown here. The challenge I am facing is making it fullscreen, where the height and width will be variable. This makes using the background-position trick ineffecti ...

Why is the Bootstrap template working on index.html but not on any other React components?

Greetings to all who stumble upon this post! Hello, I have been diligently working on a React application and am eager to integrate a Bootstrap template. However, everything seems to be in order except for the responsiveness of the template. It lacks anim ...

The webpack-bundle-analyzer tool reveals that running webpack -p does not eliminate the development dependency react-dom.development.js

Here is my custom webpack configuration: const path = require('path'); const HtmlWebpackPlugin = require('html-webpack-plugin'); const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; const SO ...

Having trouble simulating getSignedUrl from npm package "@aws-sdk/cloudfront-signer" with jest

I attempted to simulate the npm module "@aws-sdk/cloudfront-signer"'s function getSignedUrl using jest. Below is the code snippet: import { getSignedUrl } from '@aws-sdk/cloudfront-signer' let url = 'test value', // confidential k ...

What is the process for updating the color of the navigation bar?

Exploring the world of asp.net core mvc, I am curious about customizing the background color of the default _Layout navbar. <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-dark bg-dark border-bottom box-shadow mb-3"> ...

I'm having trouble getting CSS to apply to my HTML file. Any idea why?

I conducted tests in both Chrome and Firefox, ruling out any browser-related issues. My CSS has been validated and is error-free. However, when I validate my HTML code, an error message pops up stating "Bad value “stylesheet” for attribute rel on eleme ...

Vagrant Nimble Selections

Can anyone explain why my navigation bar is appearing below the header instead of beside the logo? I dedicated an entire day yesterday to designing a page with the same layout. Everything is identical in terms of design and coding, except that I initially ...

Receiving duplicate messages with Jquery after an ajax call

I am facing an issue with my user page where users can create groups to organize their content. Deleting a group is done via ajax to avoid page reloading. However, I noticed that if a user deletes multiple groups, the deletion message appears more than onc ...

The showdown: JQUERY $(document).click versus $(document).ready

$(document).ready(function(e) { if ($('#sidebar').hasClass('active')) { $('#dismiss, .overlay').on('click', function() { $('#sidebar').removeClass('active'); $('.overlay&apo ...

Use Javascript to display specific div elements by clicking a button

I could really use some assistance, When I attempt to display the select div by clicking the button, the "id" that PHP generates returns a null response in the console. The requirement is to only show the div when clicking on the "Quick Quote" button. Pro ...

Can you provide me with instructions on how to change the background image?

Having trouble with the CSS code I wrote for a background-image. It's not showing up when I set it in my CSS stylesheet. I tested it and found that it only works when written in the body... <body background="Tokyo.png"> ...but not in the styl ...

Creating fresh identifiers for table rows

I have created a dynamic data grid that can expand and collapse rows by clicking on them. It also has the functionality to add new rows with expand/collapse options using the .live() function. Currently, the ids for the parent rows and child cells are har ...