JavaScript eliminates any existing CSS styling

I am new to the world of web development and recently created a sample webpage. The page consists of an HTML file, a CSS file, and a JavaScript file. However, I encountered an issue where linking the JavaScript file to the HTML page caused the CSS formatting to be removed. This problem only occurs after adding the JS link, and everything works fine when the link is removed. I am using Windows XP SP3 and the latest version of Google Chrome. Could this be a browser-related problem?

Below are the excerpts from my code:

HTML

<!doctype html>
<html>
<head>
<script type="text/javascript" src="JavaScript.js"></script>
<link rel="stylesheet" type="text/css" href="Style.css" media="all">
</head>
<body>
<div>
<ul id="nav">
            <li id="click" <a href="#">Contact</a></li>
            <li <a href="#">Blog</a></li>
            <li <a href="#">Work</a></li>
            <li <a href="#">About</a></li>
            <li <a href="#">Home</a></li>
</ul>
</div>
</body>
</html>

Javascript

var list = document.getElementById("nav")
list.addEventListener("click", function(event) {
switch(event.target.id) {
case "click":
document.title="I changed the document title";
break;
}, false);

CSS

body {
margin: 0;
}
#nav {
    list-style-type: none;
    margin: 0;
    padding: 0;
}
#nav li {
    width: 20%;
    float: left;
    text-align: center;
    display: inline;
}
#nav li a {
line-height: 40px;
    display: block;
    padding: 0.5em 5px;
    text-decoration: none;
    font-weight: bold;
    color: #F2F2F2;
    -webkit-box-shadow: 3px 3px 3px rgba(51,51,51,0.3);
    box-shadow: 3px 3px 3px rgba(51,51,51,0.3);
}
#nav a:link, #nav a:visited {
    background-color: #071726;
}
#nav a:hover, #nav a:active, #nav a:focus {
    background-color: #326773;
}

Answer №1

Before anything else, it's important to remember to properly close your link tag. It should look like this:

<link rel="stylesheet" type="text/css" href="Style.css" media="all" />

As for the issue you are facing, it seems like there might be a problem with the JavaScript source file. Make sure to check in the development tools that your

<script type="text/javascript" src="JavaScript.js"></script>
is actually returning a script - double check that the src attribute points to the correct location of the script.

Answer №2

After some testing in JSFiddle and comparing it to a fixed version, I have converted this into an answer because I believe it may be the root of your issues:

I suggest rectifying the opening tags for <body> and <li> as they currently appear as <body and <li, respectively, without their closing brackets (>).
Once you correct this problem, your HTML should resemble the following:

<!doctype html>
<html>
<head>
    <script type="text/javascript" src="JavaScript.js"></script>
    <link rel="stylesheet" type="text/css" href="Style.css" media="all">
</head>
<body>
<div>
<ul id="nav">
    <li id="click"><a href="#">Contact</a></li>
    <li><a href="#">Blog</a></li>
    <li><a href="#">Work</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Home</a></li>
</ul>
</div>
</body>
</html>

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

Discovering the background color of see-through HTML elements using Selenium

Looking to count the characters with a specific background color. I am currently traversing through all the HTML nodes using this method: Counting inner text letters of HTML element Example HTML Page: <span style="background-color: #ffff00;"> ...

Unable to interact with the page while executing printing functionality in

component: <div class="container" id="customComponent1"> New Content </div> <div class="container" id="customComponent2"> different content </div> ...

Header getting shoved aside

My header functions flawlessly in its own HTML file, and the product details page is also perfectly fine on its own. However, when I combine the two by adding the header to the product page, it completely messes up. It pushes the header out of place, causi ...

How do I store Data along with the Heading name into my local storage using Query and Json?

$(document).ready(function () { $(":button").click(function () { var btn = $(":button").val(); if (btn == 'Favoritisieren') ...

Exploring criteria in mongodb

user.js exports.searchProducts = async (productName, productBrand) => { let queryFilters = { productName, productBrand } // ====>>> if productBrand or productName is = '' then it does not search queryFilters = JSON.parse(JSON.stri ...

Strip away double quotation marks from object attributes except those beginning with a number

I've searched extensively and reviewed various Q&A forums, but I haven't encountered a scenario quite like this. Here's an example of the object I'm working with: props: { "label": "1rem", "text3": "1rem", "text2Button": "1re ...

Utilize jQuery to insert a span element inside a paragraph element before it is appended to the DOM

My current task involves utilizing jQuery DOM elements to insert a few paragraphs into a div. The following code accomplishes this successfully: $('#detail_overlay').append($('<p />', { "class" : "detail_txt" }) .text( $(' ...

How can a JSON object be correctly generated in Rails and then fetched using jQuery/Ajax from a controller method?

I am currently developing an application that utilizes an AJAX request to fetch JSON data from a rails action. Everything appears to be working as expected, but upon debugging the code, I noticed that the JSON value I require is returning as undefined. Be ...

A guide to properly ending an HTTP connection in a node.js application with Express

Currently working with node.js (Express) utilizing server-sent events. My aim is to end the event stream by closing the sse HTTP connection. Here's the function in question: router.get('/sse', function (req, res) { }); What would be the b ...

Is IE causing issues with AJAX and document.getElementById().innerHTML?

Despite claims that I did not thoroughly research, I can assure you that I have reviewed extensively all information related to my query. Unfortunately, I have yet to find a satisfactory answer. I have implemented a basic AJAX script that is designed to lo ...

Update or overwhelm a node_module file reference

Let's say I have an installed node_module called magicalModule, which was installed using the command npm i magicalModule. To use it in my code, I import it like this: const magic = require('magicalModule'). Now, is there a way for me to o ...

Difficulty with Jquery's random selection of grid divs

I am working on a grid layout consisting of 9 divs nested in columns of three. The objective is that when the main grid (with ID #left) is clicked, two divs from the middle row (row="1") should have the class .show randomly applied to them. In the column w ...

use separate functions for each checkbox list

I currently have two sets of checkboxes, and I want to trigger a method when any checkbox is checked from either set. Here is the code for that event: $("input[type='checkbox']").change(function () { var val = $(this).val(); ...

Experiencing a RepositoryNotFoundError in TypeORM, although I am confident that the repositories are properly registered

I am creating a new application using Next.js + TypeORM and encountering an issue with the integration. RepositoryNotFoundError: No repository for "User" was found. It seems like this entity is not registered in the current "default" connection? Althoug ...

What is the best way to extract individual objects from several arrays and consolidate them into a single array?

Currently, I have a collection of objects stored in a variable called listOfObjects. They are not separated by commas because I utilized the Object.entries method to extract these values from another array. console.log(listOfObjects) outputs { q: 'L ...

Issue with submitting content using Asp.Net Core along with Vue.JS and Axios

Hello there! I'm a novice when it comes to Vue.JS and I'm facing an issue while trying to post an object using axios, as all the properties are being received as null values. Let me share my basic Model: public class Employees { public int ...

issue arising where the table's border is not displaying

I'm confused about why the border of the first tbody tr's td is not visible. https://i.stack.imgur.com/Fwlv7.png I can't see any reason why the border is not showing up. Here's what I've figured out: If the natural height of th ...

Tips for accessing the 'styled' function in Material UI ReactHow to utilize the 'styled' function

Hey there, I'm facing an issue while trying to export a styled AppBar. Check out my code below: import * as React from 'react'; import { styled, useTheme } from '@mui/material/styles'; import MuiAppBar from '@mui/material/AppB ...

ReactJS component not triggering OnChange event in IE 11

While exploring the React.js documentation, I came across a suggestion to use the onChange event for text areas. Interestingly, when I tried pasting some text into an empty IE 11 text area, the onChange event failed to trigger. Surprisingly, it worked perf ...

Angular 2 Error: When used in strict mode functions or arguments objects, the properties 'caller', 'callee', and 'arguments' may cause a TypeError

I'm facing an issue with Angular 2 that seems to be a common problem, but I haven't been able to find a solution yet. I've created a service that is called from another Component, which works fine. The problem arises when I try to make an h ...