Using JavaScript to Implement a Scrolling List

Currently, I am diving into the world of coding with HTML, JavaScript, and CSS for a college project. Unfortunately, my instructors aren't providing any assistance, so I'm turning to you for help.

I have managed to put together some code to create a scrolling list that moves up and down with mouse clicks.

I've created three separate files for HTML, CSS, and JavaScript. However, it appears that the JavaScript file isn't properly linking. Can you lend me a hand?

HTML (named ola.css):

 <body>
 <head>

<link type="text/css" rel="stylesheet" href="ola.css" />
<script type= "text/javascript" src="java.js"></script>
<div class="box">
        <input type="button" value="Scroll" id="scroll" />
        <ul>
            <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</li>
            ...
            
        </ul>
    </div>

</head>
</body>

CSS:

#scroll {
    position: fixed;
    padding: 5px 10px;
}
.box{
height: 200px;
    overflow: auto;
}

JAVASCRIPT (named java.js):

<head> 

<SCRIPT LANGUAGE="JavaScript"></script>


<script type="text/javascript"></script>

$(document).ready(function () {
    $('#scroll').click(function () {
        $('.box').animate({
            scrollTop: '+=100'
        }, 100);
    });
});



</head> 

I stumbled upon this code online, but unfortunately, when I test it, everything seems to be in order except for the scrolling functionality when clicking the button.

Answer №1

Avoid including HTML tags within your JavaScript file. Instead, simply write the following code:

$(document).ready(function () {
    $('#scroll').click(function () {
        $('.box').animate({
            scrollTop: '+=100'
        }, 100);
    });
});

Prior to utilizing jQuery, it is essential to have a solid understanding of JavaScript basics. It's crucial to ensure that even the structure of your HTML file is correct (as pointed out by others).

Answer №2

Ensure that your JavaScript code is enclosed within the script tags and not positioned after it.

Answer №3

Make sure to include jQuery in your head tag for proper functionality. If you are using jQuery, it needs to be added.

Remember to add this code snippet to your head tag:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

Your body tag seems to be out of place.

Check out a demo of your code here: Demo

Answer №4

I am unable to locate the jQuery JavaScript file in your code. You can include one of the following options:

- Latest version, hosted by jQuery http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js - Version 1.x family, hosted by Google http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js - Version 1.9.1 family, hosted by Google - Version 1.9.1, hosted by Microsoft

Referenced from HERE

Also, please note that your head and body tags need adjustment:

<head> 
    Include your CSS link and JavaScript here
</head>
<body> 
   Construct your webpage content here
</body>

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

Link to download an Excel spreadsheet

My server is capable of generating excel files dynamically, and I am utilizing AJAX to download these dynamic excel files. Upon successful completion in the callback function, I receive the data for the excel file. $.ajax({ url: exporting. ...

Nav Bar Toggle Button - Refuses to display the dropdown menus

My Django homepage features a Bootstrap navbar that, when resized, displays a toggle button. However, I am experiencing difficulty with dropdown functionality for the navbar items once the toggle button is activated. Can anyone provide guidance on how to a ...

What is the method for obtaining the properties of a type as an array in Typescript?

In the given scenario: class Foo { constructor( private one: string, private two: string, private three: string) { } } Is there a way to create an array containing the type's properties? For example, I need to gene ...

extracting an attribute from a class's search functionality

Let's consider the following HTML structure: <div id="container"> <div> <div class="main" data-id="thisID"> </div> </div> </div> If I want to retrieve the value inside the data-id attribute ...

Utilize a single WebAssembly instance within two separate Web Workers

After compiling a wasm file from golang (version 1.3.5), I noticed that certain functions using goroutines are not supported. When these functions are called, they run in the current thread and slow down my worker significantly. To address this issue, I w ...

Creating a horizontal slideshow for multiple divs can be achieved through various methods,

I'm feeling a little lost when it comes to creating a slideshow similar to this image: (Symbian^3 music menu) https://i.sstatic.net/oK0F2.png Can anyone provide me with a straightforward code example or a helpful resource to achieve something simila ...

Troubleshooting issues with media queries related to device pixel ratio

My media queries in LESS are functioning correctly on the desktop, but encountering issues when tested on an iPad2 or Samsung Galaxy 4 phone. It appears that the device-pixel-ratio is causing disruptions. I attempted to reduce the min-device-pixel-ratio to ...

Removing an individual HTML element within a form using JavaScript Fetch() in the presence of multiple components

The Situation Having a form that includes image components generated from a MySQL database with PHP, I've implemented javascript fetch() functionality on different pages of the website to enhance user experience. However, in cases where the functiona ...

Styling Dynamic HTML Content in Angular: Tips and Tricks

After running this line of code, I have a generated element sub with a property of [sub]. <li [displayMode]="displayMode" template-menu-item style="cursor: pointer" [routerLink]="['/request/checkoutReview']" icon="fa-shopping-cart" name ...

The CSS styling does not seem to be applying correctly within the create function of

I'm a Laravel beginner and I've been following the Bitfumes Laravel series on YouTube: Laravel - Create Blog and Admin Panel Everything was going well, until I added the following line in the index function: public function index() { return ...

Display the list at the top using <ul> tag

I am struggling to create a <ul> list with a specific width of 50%. When the text spans more than one line, it is causing an unwanted margin at the top of the second LI element. Please refer to the screenshot linked below for visual clarity. https:/ ...

Bulma's Grid System Spans Columns Across the Entire Viewport

Utilizing the Bulma CSS framework. In my attempt to arrange some elements, I am puzzled as to why, in a desktop view, the items do not transition to the next row once they exceed the viewport. Below is a snippet of code along with a link to a JS Bin exam ...

Using Elasticsearch in conjunction with Node.js - All I'm trying to do is display my query on the console

How can I log the elasticsearch query while working with nodejs? client.search({ index: 'my-index', query: { match_all: {} } } In the code above, I am only interested in extracting the index and query from the body. Specifically, I am focusi ...

The animation in Rive feels sluggish when navigating to a page with animation in Blazor WASM, despite implementing dispose methods

After attempting to display river animation on the index page using Blazor WASM (basic template), I encountered some performance issues. When navigating back and forth between the Counter page and the index page, I noticed that after around 20 clicks, the ...

In React, the ES6 prototype method map failed to render anything on the screen

Is there an issue with my map method implementation? var App = React.createClass({ getInitialState(){ return { items:[1,2,3] } }, renderItem(){ return( this.state.items.map((item,i))=> <li key={i}> { ...

Are mutations in Vuex guaranteed to be atomic?

I'm currently investigating the atomicity of mutations in Vuex. The code snippet I'm reviewing has me questioning whether the CHANGE_A mutation could potentially be triggered while CHANGE_B is still in progress: const mutations = { [CHANGE_A]( ...

Exploring the Possibilities: Incorporating xlsx Files in Angular 5

Is there a way to read just the first three records from an xlsx file without causing the browser to crash? I need assistance with finding a solution that allows me to achieve this without storing all the data in memory during the parsing process. P.S: I ...

Tips for utilizing Vue router query parameters while in hash mode:

Is there a more efficient way to access URL parameters in Vue methodology, without having to rely on window.location.href and parsing the URL? router/index.js const router = new Router({ mode: 'hash', routes: [] }); router.beforeEach((to, f ...

What is the best way to deselect all "md-checkboxes" (not actual checkboxes) on an HTML page using a Greasemonkey script?

After spending a frustrating amount of time trying to disable the annoying "md-checkboxes" on a certain food store website, despite unchecking them multiple times and reporting the issue without any luck, I have come to seek assistance from knowledgeable e ...

Is Firefox the only browser where the webpage is displayed off-center?

I've encountered a strange issue with my website that I can't seem to figure out. Despite searching extensively on Google and Stackoverflow, I haven't come across anyone else facing the same problem. During testing in different browsers lik ...