Showing different HTML elements based on the link that is clicked

Recently, I delved into the world of web development and decided to test my skills by creating a basic webpage with an interactive top navigation bar. Depending on the link clicked, specific HTML elements would be displayed while turning off others using a switch statement.

However, things didn't go as planned.

Take a look at the HTML code snippet below:

<!DOCTYPE html>

<html>
<head>
  <title>Care and Advice Clinic</title>
  <script type="text/javascript" src="backend.js"></script>
  <link rel = "stylesheet" type = "text/css" href = "stylesheet.css">
</head>

<body>
  <ul>
    <li><a href="#" onclick="javascript: StateMachine(1); return false;">
      Contact and Address
    </a></li>
    <li><a href="#" onclick="javascript: StateMachine(2); return false;">
      Operation Times
    </a></li>
    <li><a href="#" onclick="javascript: StateMachine(3); return false;">
      About
    </a></li>
    <li><a href="#" onclick="javascript: StateMachine(4); return false;">
      School of Diabetes
    </a></li>
  </ul>
  <h1 style = "font-size: 40px; padding-top: 30px;"> Diabetes Care and Advice</h1>
  <p id="Debug" style="font-size: 60px;"></p>
  <p id="Contact" style="font-size: 60px;">
    You have clicked on Contact and Address
  </p>
  <p id="OpTimes">You have clicked on OpTimes</p>
  <p id="About">You have clicked on About</p>
  <p id="School">You have clicked on School</p>
  <script>javascript: StateMachine(0);</script>
</body>
</html>

And here is part of the JavaScript code responsible for this behavior:

//The states are Contact (1), OperationTimes (2), About(3), School(4), home(0)
StateMachine(index);

function StateMachine(index){

  switch(index){

    case 0:
      document.getElementById("Debug").innerHTML = "Switch case 0 entered";
      document.getElementById("Optimes").style.display = 'none';
      document.getElementById("About").style.display = 'none';
      document.getElementById("School").style.display = 'none';
      document.getElementById("Contact").style.display = 'none';
      break;

    // other cases omitted for brevity...

}

While the debug message displays correctly, the switch statement fails to execute. Something seems to be amiss.

If anyone can provide guidance on where I might be going wrong, it would be greatly appreciated!

Answer №1

After testing this code on Firefox 54, I can confirm that it successfully executed. However, I encountered an error message due to the inconsistent casing of OpTimes in your HTML code. To fix this issue, make sure to standardize the usage of OpTimes or Optimes throughout.

Answer №2

Modify the

<p id="OpTimes">You have clicked on OpTimes</p>
to
<p id="Optimes">You have clicked on OpTimes</p>
.

The id of this element should be Optimes instead of OpTimes.

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

Problems with radio button serialization in jQuery form plugin

I've created a basic form: <form class="dataform" method="post" id="settings" action="/"> <input type="radio" name="shareSetting" value="n"/> <input type="radio" name="shareSetting" value="y"/> <input type="button" na ...

the function does not output any value

I am currently facing an issue with my function called IsValidUrl(). This function is supposed to return values based on a certain condition (either false or true). However, there seems to be another function within it that is preventing the values from be ...

Learn about Angular8's prototype inheritance when working with the Date object

In my search for a way to extend the Date prototype in Angular (Typescript), I stumbled upon a solution on GitHub that has proven to be effective. date.extensions.ts // DATE EXTENSIONS // ================ declare global { interface Date { addDa ...

Retrieving distinct items from an array containing nested objects and sub-properties

In my script, I am attempting to retrieve unique objects based on the state from an array of objects. The current script is functioning correctly for most cases. However, there is one scenario where a nested object in a specific state has a sub-state, and ...

Remove the initial 15,000 lines from a text file using Node.js

Looking for a way to delete the first 15,000 lines of a large text log file (roughly 20MB) using Node.js. Any suggestions on how to accomplish this task? ...

The async and await functions do not necessarily wait for one another

I am working with Typescript and have the following code: import sql = require("mssql"); const config: sql.config = {.... } const connect = async() => { return new Promise((resolve, reject) => { new sql.ConnectionPool(config).connect((e ...

Dynamically change the state based on intricate layers of objects and arrays

Here is the issue I am facing I am attempting to update the state of an object named singleCarers I have the index of the roster in the rosters array that I need to update However, the key monday: needs to be dynamic, as well as the keys start: and fini ...

Encountering an error when attempting to access the 'path' property of undefined from a user-defined input file in Bootstrap 4

I'm currently working on a form that includes a custom input group for selecting a local image file. Unfortunately, I am having trouble retrieving the file name in my server code. Despite checking req.body and trying req.file.path, I have been unable ...

How to turn off automatic formatting in CKEditor

Whenever data is entered into a field using CKEditor, the Save button becomes enabled. However, if I programmatically set data into the field using Javascript, the Change event always triggers causing the Save button to remain enabled even if the field is ...

Is it possible to apply search filters within a child component in Angular?

I have a situation where I am passing data from a parent component to a child component using *ngFor / @input. The child component is created multiple times based on the number of objects in the pciData array. pciData consists of around 700 data objects, ...

Problem with Ajax causing full-page reload

I currently have a webpage that utilizes JqueryUI-Mobile (specifically listview) in conjunction with PHP and some Ajax code. When the page loads initially, it displays a list generated from a MySQL DB. I want this list to refresh itself periodically witho ...

Properly defining a DI service in Angular 2.1.2 using ES5

I have created an Angular 2 service that utilizes the Http service and is accessed by other components. However, I am unsure if my implementation is correct: (function (app) { 'use strict'; app.LoaderService = ng.core.Component({ providers: ...

Testing a React component to ensure that it correctly handles clicks occurring outside

Utilizing the solution found in this particular answer to address clicking outside of a component: componentDidMount() { document.addEventListener('mousedown', this.handleClickOutside); } componentWillUnmount() { document.removeEventLis ...

Using async/await in React to retrieve data after a form submission

I am currently facing an issue with displaying data fetched from an API on the screen. My goal is to retrieve data when a user types something in a form and clicks the submit button. The error message I am encountering is: TypeError: Cannot read propert ...

"Fill the designated area on the webpage with data retrieved from an external script

In my current project, I am utilizing Angular and JQuery to enhance re-usability by setting the header and footer as partials. However, I encountered an issue where I needed certain javascript functions to be executed within the footer upon loading - a tas ...

What is the best way to incorporate code into a page with numerous conflicting CSS styles, without resorting to Scoped CSS?

Struggling to incorporate tab code into a page with conflicting CSS? Even after attempts to edit classes and labels, the original code fights back against the new additions. While scoped CSS seems like the perfect fix, it's not widely supported yet. ...

What are the best practices for utilizing the "this" keyword with fetch?

After extensively studying ES6 documentation and the changes it brings, I am eager to incorporate the new oop syntax along with modern libraries such as fetch. Below is the code snippet I have been working on: apiCall(url, thisAlias=this){ fetch(url). ...

Checking with jQuery Validate: displaying an error message if input matches a certain value

I spent 6 hours trying to figure out a solution to my issue. Currently, I am utilizing the jQuery Validation plugin. In my form, there is a password input field. When an AJAX request detects an incorrect password, another input field is set to a value of ...

Spacing before input text is found to be unnecessary and can be removed

Hello there, I've encountered a slight issue with a text input field that has border radius. The first character typed seems to protrude slightly outside the input field due to the border radius. Is there a way to add some extra whitespace before the ...

Tips for utilizing a for loop within an array extracted from a jQuery element for automation

I am looking to streamline the process using a for loop for an array containing 10 image files in the parameters of initialPreview and initialPreviewConfig. My envisioned solution involves the following code snippet: for (i = 0; i < 11; i++) { "< ...