Utilize jQuery to apply a CSS class to the element that is clicked and inject additional content into a designated div

Here is a link to a page where I am experimenting with creating an interactive choose-your-own-adventure story:

My goal is to add the class "disabled" to the element when a user clicks on the word "stairs" and then continue the story. Below is the relevant code snippet extracted from the page:

<div id="ongoingStory"><p>This wasn't where you meant to go.</p>
   <p>You were walking, and thinking, and now that you've come back to the present, you're looking at brick on two sides and a slatted, padlocked gate between them.  You've come up an alley.  Or down one.</p>
   <p>Up a set of <a href="#" id="stairs">stairs</a> bolted precariously to one building's brick side, there's a door.  Above it, a sign juts out: <span class="smcaps">Sparrow Aisling / Psychic</span>.  Barely readable in the dusk, but there it is.</p></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="narrative.js"></script>

and excerpt from narrative.js --

var climbStairs = "<p>You climb the stairs and go through the door.</p><p>Inside, there's a red velvet sofa, diamond-tufted, and an ottoman to one side.  Heavy green curtains and a spray of tiny, ornately framed <a href=\"#\">mirrors</a> occupy the back wall.  Occult clutter fills the rest of the room — candles in cups, crystals, brass bells and bowls, figurines from a number of different religions, a little round tea set, and bundles of dried herbs and sticks.</p><p>The obligatory cat, gray, looks up at you, twitches its tail, and goes back to staring out the room's one window.</p><ul><li><a href=\"#\"> Sit and wait.</a></li><li><a href=\"#\"> Nose around while you wait.</a></li></ul>"

$( document ).ready( function() {
   $( "#stairs" ).click( function() {
      $( this ).addClass( "disabled");
      $( "#ongoingStory" ).append( climbStairs );
   });

});

It has been a long time since I attempted anything more interactive than a menu bar, so please bear with me. I may have made errors in the syntax or order of things, failed to escape something that needed escaping, or overlooked something very basic.

Answer №1

Your posted code appears to be correct, however there is a slight issue on your webpage with the following line:

var sit = "<p>You find yourself standing in front of an old wooden door. Your heart races as you reach for the doorknob. Taking a deep breath, you turn it and push the door open.</p><p>Inside, you see a dimly lit room filled with mysterious artifacts and treasures. The air is thick with dust, giving the room an eerie feel</p><p>As you step further into the room, a sudden gust of wind slams the door shut behind you.</p>

It seems like a closing " is missing at the end.

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

Steer clear of posting additional content that might bump the existing content further up

How can I keep the search bar centered on the screen, even when content is added below it? Here's a screenshot illustrating the issue: https://i.stack.imgur.com/7pQ1q.png The text in the image is causing the search bar to move up, but I want the sea ...

Pressing the Button Increases the Counter, However the Counter Resets After

My goal is to create a basic counter with increment and reset buttons. When I click on the increment button, the counter properly goes from 0 to 1 (I can confirm this by checking the console log in Chrome). The issue arises when, after incrementing, the c ...

Leveraging server-side data with jQuery

When my client side JQuery receives an array of JSON called crude, I intend to access and use it in the following way: script. jQuery(function ($) { var x = 0; alert(!{JSON.stringify(crude[x])}); ...

Achieve full height in Grid component of material UI by eliminating margins

In this codesandbox example, I have successfully set the grid container height to full using 100vh. However, there is an issue with a margin of 8px being added by the user agent to the body element, and I'm struggling to find a solution to remove it. ...

Remove the bootstrap styling from a section of the webpage

I am in the process of setting up a preview box for an HTML editor on one of my pages. I created a basic <div id="preview"></div> style container where I occasionally input my HTML source, which is working adequately. The issue arises when Boo ...

When attempting to input data into the database, an error is displayed stating that /test.php cannot be POSTed

Every time I try to input data using PHP, it throws an error Cannot POST /test.php. I've been attempting to fix it with no success. Can anyone please help me solve this issue? It's crucial for my project work. Here is my HTML code: <html> ...

Error occurred while fetching image from Medium story API in Next.js

While working on my Next.js app, I encountered an issue with the Medium story API. Every time I try to fetch and display an image using the API, I receive an error message stating "upstream image response failed." The specific error code is: upstream image ...

Splitting the page into four sections with a unique H layout using CSS styling

Attempting to create an H page layout: body { background-color: grey; background-size: 100%; background-repeat: no-repeat; } html, body { height: 100%; margin: 0px; } .a { float: left; width: 25%; height: 100%; border: 1px solid blue ...

The Jqgrid is not displaying any information from the JSON file

I'm currently engaged in a project using Codeigniter. My aim is to present some data in jqgrid retrieved from json using jquery. Despite no errors or exceptions, Jqgrid fails to display the data. $(document).ready(function(){ $("#company").click ...

Select an image from the browser using JavaScript, adjust its size, and then transmit it to the server. Finally, utilize PHP to properly save the image

I am currently tackling the challenge of implementing a classical feature where users can select a file in their browser ("Browse"), have JavaScript resize it to a maximum width/height of 500 pixels, upload it to the server, and then save it to disk using ...

Using Javascript within HTML: A guide on when to utilize semi-colons and when to implement the return false statement

Exploring the use of JavaScript attributes within HTML elements: <input type="button" onclick="somceFunc()" /> <input type="button" onclick="somceFunc();" /> <input type="button" onclick="somceFunc(); return false;" /> Debating whether ...

What is the process for developing multiple screens or views in PhoneGap?

Currently working on a reading app and decided to give phoneGap a shot for the first time. I'm pretty proficient in HTML, CSS, and JS, but not very familiar with xCode. In this app, I plan to have a button on the home screen that redirects users to an ...

Is it possible to create a CSS1 sprite animation triggered by a mouse hover

Is there a way to create sequential image loading on mouse hover for a CSS1 Sprite, similar to an animation? Please refrain from suggesting CSS3 solutions as not all browsers fully support CSS3 animations. Thank you. ...

Seeing the Bootstrap css load immediately upon the page loading and switching between pages

Upon loading my webpage or navigating between subpages, I've noticed that the Bootstrap.css styles are interfering with my CSS transitions. For some reason, my regular links appear orange initially, but they turn blue during loading or when transition ...

Organize Events by Date with KnockoutJS

Recent Development: The latest update includes me providing a solution to my issue. If you have any suggestions on how to enhance the readability and efficiency, please feel free to share. Issue: I am encountering a problem while using fullcalendar.io in ...

GitHub jQuery Page

Unable to display images based on user choice from the dropdown box, which is causing an issue after committing to GitHub website. There are no errors shown in the developer tool, but when hovering over the image link it says 'Could not load the imag ...

When a textarea contains a new line, it will automatically add an additional row and expand the size of the textarea

Developed a form with the functionality to move placeholders in a textarea upwards when clicked. The goal is to increment the height of the textarea by 1 row for every line break. However, I am uncertain about what to include in the if statement of my Ja ...

What is the best way to style multiple classes within the same element (specifically an ordered list ol

Using CSS counters, I have created various styles for an ordered list <ol> by applying different classes and adjusting the styles accordingly. Now, I want the counters to adjust based on screen size, displaying smaller numbers on smaller screens. To ...

Refreshing the page in Next.js causes issues with the CSS classNames

I am currently in the process of migrating a React SPA to Next.js, and I am relatively new to this framework. The issue I am encountering is that when I initially load the Home page, everything appears as expected. However, if I refresh the page, incorrect ...

Incorporating a divider into a Material-UI MenuItem causes an additional border

I needed a way to separate the MUI MenuItem using a divider, but it resulted in an extra border around the item where the divider was placed. The highlighted section in the image shows the item that has the divider= true setting and the extra border. Is th ...