My website is experiencing issues with incorporating jsfiddle code

Snippet:

<div id="container">
<div id="bar">
</div>
</div>

CSS Style:

#container {
width: 20px;
height: 150px;
position: relative;

}
#bar {
width: 100%;
height: 100%;
background-color: red;
position: absolute;
bottom: 0px;
}

Javascript Logic:

function Bar(element) {
this.barEl = element;
this.progress = 100;
}

Bar.prototype.setProgress = function (p) {
if(p >= 0) {
    this.progress = p;
    this.barEl.style.height = this.progress + "%";
}
}

var barObj = new Bar(document.getElementById('bar'));
setInterval(function () {
barObj.setProgress(barObj.progress - 10);
}, 1000);

The code snippet can be viewed on jsfiddle here. The code is not functioning correctly when copied and pasted, which may be due to an onLoad issue. Attempting to use $(document).ready(function() was unsuccessful in solving the problem.

Answer №1

Make sure to include the jQuery script in the <head> section of your code:

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

Alternatively, you can place the code at the end of your document so it only runs once the entire page has loaded.

Below is a basic example demonstrating how to incorporate this code (typically, HTML, CSS, and JS should be separated), which you can compare with your own implementation:

<!DOCTYPE html>
<html>
    <head>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

        <title>- It Works!!! -</title>

        <style>

        #container {
            width: 20px;
            height: 150px;
            position: relative;   
        }

        #bar {
            width: 100%;
            height: 100%;
            background-color: red;
            position: absolute;
            bottom: 0px;
        }

        </style>

    </head>
    <body>
        <div id="container">
            <div id="bar">
            </div>
        </div>
    </body>

    <script type="text/javascript">
        function ProgressBar(element) {
            this.barEl = element;
            this.progress = 100;
        }

        ProgressBar.prototype.setProgress = function (p) {
            if(p >= 0) {
                this.progress = p;
                this.barEl.style.height = this.progress + "%";
            }
        }

        var progressBar = new ProgressBar(document.getElementById('bar'));
        setInterval(function () {
                progressBar.setProgress(progressBar.progress - 10);
        }, 1000);

    </script>
</html>

I trust this guide will assist you!

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

The Angular routing feature seems to be malfunctioning

I have encountered a frustrating issue with AngularJS routing and despite countless searches for solutions, I am still facing the same problem. In my root folder at c:\intepub\wwwroot\angular\, I have three files that I am testing under ...

Issues with React's useState hook not updating properly

As a React beginner, I am facing an issue with my input field filtering functionality. The problem arises when the results are filtered and rendered, as it seems to be ignoring the last character of the input value. For instance: If I type in "s", nothing ...

Unable to render nested HTML tag in simple React.JS component

I'm currently working on setting up my first component. After following a tutorial, I tried implementing their code to create the component. The code does inject the first <div> into the HTML document, but the <h1> element is missing. In ...

Sending a Thunk to the store using Typescript

Within my primary store.ts file, the following code is present: const store = createStore( rootReducer, composeWithDevTools(applyMiddleware(thunk)) ); store.dispatch(fetchUser()); Upon initial rendering, an action is dispatched to fetchUser in ord ...

Does this CSS identifier align with the specifications?

There appears to be an issue. The styles assigned to the second element should change when hovering over it, only if the third element has the class .active. CSS: li:nth-child(3).active ~ li:nth-child(2):hover HTML: <ul> <li> < ...

Creating columns of equal height using Bootstrap 3 and flexbox

Working with Bootstrap 3 (unable to switch to Bootstrap 4) and looking to achieve the same-height columns effect as shown in the image: https://i.sstatic.net/WR55a.jpg Managed to get it done using position absolute and @media screen for image size and padd ...

Angular's observable function is not providing a complete response

In my Angular component, I have a function that is called from a template. This function returns an Observable of type string, but unfortunately it only returns the `data` variable. How can I modify it to return `dateNew[0] + " de " + data + " de "+ dateNe ...

I keep receiving an error message that says "The MIME type 'application/json' is not executable." Can anyone explain why this is happening?

Encountering an issue because the MIME type of this file ('application/json') is non-executable and strict MIME type checking is turned on. I am working on incorporating a map into a website and have obtained a JSON file containing coordinates. ...

Having trouble with Select2 functionality within a modal in Bootstrap version 4.6.0?

I've recently started working with bootstrap 4 and I'm already on the lookout for alternatives to solve this issue. Both select2 elements are functioning normally but when I place them inside @include() in the modal tab-content, they behave diffe ...

Generate visual representations using Google charts and JSON data

I am looking for a way to retrieve and utilize a dataset for Google Charts from a separate JSON file. I attempted to use jQuery getJSON method but encountered issues. The goal is to have Google Viz utilize the JSON data to create a bar chart. Is there a ...

Vue snapshot testing is encountering a failure with the error message "TypeError: Cannot read property of undefined"

Everything seems to be working fine with the component on the page without any errors. Storybook is also functioning well, but the problem lies in the unit test. import { mount } from '../../vue'; import { createLocalVue } from '@vue/test-u ...

ERROR: Unable to use import statement outside a module in REACT-APP

Every time I attempt to print it out on a browser, I encounter an error in the header saying "Uncaught SyntaxError: Cannot use import statement outside a module." Prior to posting this question, I tried various solutions related to this issue, such as addi ...

The reference to the jQuery object is void

let quiz = { settings : { questionTypeDropDown : $j('#questionTypeDdown') }, data : { questionType : 'test' }, initialize: function() { quiz.bindUIActions(); }, bindUIActions : fun ...

Retrieving JSON data in array by making an AngularJS resource call

Developed a factory that returns JSON data and calling it from the controller, but the data is returning empty. I'm not sure where I went wrong. There are no console errors and in the network tab, the JSON data is also loading. 'use strict&apos ...

What is the best way to swap out elements in my string with those from an array?

Struggling to replace special characters in file names before saving them to the Windows filesystem due to compatibility issues. For my initial approach, I used the replace() method repeatedly on the string to replace specific special characters. Here&apo ...

Using Regular Expressions in JavaScript to verify if an element from an array is contained within a string

Looking for a simple JavaScript code for a Vue application that will split the string into an array and check if any value is present in a different string. Here's what I have: let AffiliationString = " This person goes to Stony Brook" ...

The Autocomplete component from MUI is alerting me to provide a unique key for every child element being passed

I am currently using the Autocomplete component from MUI and encountering an issue with a warning that says: Warning: Each child in a list should have a unique "key" prop. Although I've added keys to both renderOption and renderTags, the wa ...

Utilizing XMLHttpRequest in JavaScript to interact with Elasticsearch

Having trouble making a GET request to another website from work, but when attempting to request from Elasticsearch using localhost it is not working. The status codes returned are: Create Object from JSON String 4, 0, readyState = 4 status = 0 Below i ...

Guide to developing a personalized useReducer with integrated decision-making and event activation

I am interested in creating a custom hook called useTextProcessor(initialText, props). This hook is designed for managing and manipulating text (string) within a React state. It utilizes useReducer to maintain a cumulative state. Here is the implementation ...

Shadows with pixel art style in Threejs

I developed an application where I dynamically created shelves. Everything is working fine except for the shadows. I'm not sure if the issue lies with the lighting or the objects themselves. Can anyone provide some assistance? Here is a snapshot showc ...