The jQuery seems to be malfunctioning, perhaps due to an issue with how it was incorporated

As a newcomer to this community and to jQuery/JavaScript, I have spent a considerable amount of time researching without finding a solution to my problem.

I attempted to create functionality where clicking on a specific element changes its class.

The important parts of my code include:

HTML:

<div class="taskbar">
        <div id="taskbar-start" class="taskbar-start-inactive">
            <img class="taskbar-start-logo" src="img/logo.png" /> 
            <div class="taskbar-start-text">Start</div>
        </div>
</div>

CSS:

#taskbar-start {
    margin-top: 4px;
    margin-bottom: 2px;
    margin-left: 2px;
    width: 90px;
    height: 33px;
    background-color: #C2C5CA;
    cursor: pointer;
}
.taskbar-start-inactive {
    box-shadow: -1px -1px 0px 0px #000 inset, 1px 1px 0px 0px #FFF inset, -2px -2px 0px 0px #868A8E inset;
}
.taskbar-start-active {
    box-shadow: -1px -1px 0px 0px #FFF inset, 1px 1px 0px 0px #000 inset, 2px 2px 0px 0px #868A8E inset;
}

JavaScript

$('#taskbar-start').click(function() {
    $(this).toggleClass('taskbar-start-active');
    alert("Yeah!");
});

However, this code does not work. I did not want to download jQuery as I plan to use this code as a theme on Tumblr, where downloading it may not be ideal. Currently, I have included jQuery and my JavaScript file like this:

<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="javascript.js"></script>

I tried a test code to check if jQuery is loaded:

window.onload = function() {
if (window.jQuery) {  
    // jQuery is loaded  
    alert("Yeah!");
} else {
    // jQuery is not loaded
    alert("Doesn't Work");
}}

The test code seems to work, but my actual code doesn't. When I paste the entire code here and run it, it works fine, so the issue might be related to including jQuery?

Please excuse me if my question sounds silly, I'm very new to all of this.

Answer №1

To ensure your code runs smoothly, always remember to place it within the document.ready function:

<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="javascript.js"></script>
<script>
    $(document).ready(function(){
        $('#taskbar-start').click(function() {
            $(this).toggleClass('taskbar-start-active');
            alert("Success!");
        });
    });
</script>

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

Utilizing Ajax Requests to Access Services in Zend Framework 2

After my previous inquiry about How to access a variable in my Service, I am now facing challenges accessing the Service from my View Helper through an Ajax Request. I have included a new method in the BookmarkAdvertService() to add a Bookmark. publi ...

Unveiling the secrets of Angular ng-messages: Tips for validating password confirmation!

THE ISSUE: Incorporating the ng-messages directive for real-time validation in my angular application has been successful so far. However, I am facing a challenge with validating the 'password confirmation' field. CODE SNIPPET: <form name=" ...

What is the best way to display swipebox thumbnails in a grid layout instead of a single column?

I'm a novice in the world of web design and attempting to organize a group of 70 thumbnails into a grid or columns within an existing layout for a friend. Currently, they are displayed in a single lengthy column. Is there a straightforward way to adju ...

The page background appears to be incomplete in its coloring

As I work on creating a web application for personal use, my goal is to ensure it is mobile-friendly. However, I've encountered an issue where the background of my language page isn't displaying in full blue color as intended. Despite trying diff ...

Create an HTML button with dual functionality

I currently have a button in my HTML that triggers the opening of the sidebar on mobile devices. <a href="#header" class="button scrolly">Contact Me</a> There is also a label element that, when clicked, selects the name field in the contact f ...

Exploring the Possibilities of Combining Hover Effects with Unique CSS

I'm encountering difficulties making the :hover state work with a CSS pie slice that I created. Using transparent borders and the border-radius property, I managed to style my div to resemble 1/4 of a pie. But no matter what styles I attempt for the h ...

Spotlighting incorrect form entries

I've been in the process of developing a form that includes fields for usernames, passwords, and more. I'm looking to implement validation when the SUBMIT button is activated. My goal is to trigger an alert based on the border color. When all fi ...

Sorting feature fails to function properly when used in combination with pagination and

<table> <thead> <tr> <th class="col-md-3" ng-click="sortDirection = !sortDirection">Creation Date</th> </tr> </thead> <tbody> <tr dir-paginate="item in items | filter:itemFilter | items ...

Disable browser autocomplete for Material-UI TextField

I am currently using Material UI version 0.20.0 and I am facing an issue where I need to prevent the password from being saved for a user in a TextField. I tried adding props to the TextField with autocomplete='nope' since not all browsers suppor ...

Create HTML files that generate a log.txt document

It seems like every time I try to find a solution for writing files with PHP or JavaScript, I end up in a never-ending argument. Some say it's impossible, while others claim it's possible under certain conditions. Unfortunately, none of the code ...

Tips for changing array items into an object using JavaScript

I am working with a list of arrays. let arr = ["one","two"] This is the code I am currently using: arr.map(item=>{ item }) I am trying to transform the array into an array of sub-arrays [ { "one": [{ ...

What is the best way to alternate between <div> elements?

<select id="typeselect" name="value" onchange="OnChangeSelect()" > <option value="1">General</option><br /> <option value="2">Featured</option><br /> </select> <div id="formbody" c ...

The issue arises when jQuery functions fail to function properly due to data being called after the

What I am about to bring up may present a challenge, but I will do my best to explain it clearly. I have designed my website with only one main webpage (index.php). The HTML and content for various pages are saved in a PHP script. So, when a user chooses ...

Is there a way I can put together this CSS code?

I am currently using Socialengine (Zend Framework) along with several plugins. However, one of the plugins has its own unique CSS style. There are multiple CSS cascades in use, but they are not less or sass files. The contents of my file style.css are as ...

The issue of Markdown not being recognized inside nested divs

As I develop a Bootstrap-themed website with Jekyll, I've encountered an issue when formatting content in markdown. My goal is to organize my posts using Bootstrap grids like this: <div class="row"> <div class="col col-md-6" markdown="1"&g ...

What is the best way to remove certain elements from a jQuery selection?

Here is my jQuery code: $("ul.dropdown ul").slideDown("slow"); $("ul.dropdown ul ul").children().hide(); I want the first set to be displayed without the brief display of the second set. How can I achieve this by excluding the second set from the first se ...

Solution for displaying as a table cell in Internet Explorer 6 and 7: Workaround

I am currently working on creating a single-row CSS table with multiple cells, aiming to vertically center text within each cell. The desired table layout is as follows: <table width="100%" height="54" border="0" bgcolor="red"> <tr> <t ...

How can I achieve a smooth transition effect for an image using JavaScript

Hey there! I'm currently working on a project where I have multiple buttons that, when hovered over, change the background-image of a specific div. However, instead of the images suddenly appearing on the screen, I would like them to fade in gradually ...

The Azure SpeechSynthesizer encountered an error due to using HTTP instead of the expected HTTPS protocol. This issue can be resolved by installing the NPM package `microsoft

Recently, I have been encountering an issue with the microsoft-congnitiveservices-speech-sdk npm package. Everything was working smoothly until randomly an error started popping up. This could be due to a browser update or a minor package update - I'm ...

Dealing with Cross-Origin Resource Sharing problem in serving static images on Dotnet Core 3.1

I have my dotnet core 3.1 API hosted with Angular on a different domain than my frontend HTML site. I am using a jQuery watermark plugin to add watermarks to my images, but sometimes the image displays normally without the watermark being added. Upon check ...