Modify the text in a paragraph by clicking on a link using JQuery and HTML

I'm attempting to implement a straightforward action, but I'm facing some challenges. My navigation bar consists of a few links followed by a text section. What I aim for is that when I click on a link, the paragraph of text should change to reflect the corresponding link.

I have experimented with various approaches (please excuse my rusty skills in JQuery and javascript) but nothing seems to work!

I am open to exploring new methods to achieve the desired outcome.

var ptext;

   $(document).ready(function(){
      ptext = $("#pchange");
      $(".one").click(function(){
        ptext.html("text1");
      });
      $(".two").click(function(){
        ptext.html("text2");

      });
    });

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
* {
    box-sizing: border-box;
}

body {
    font-family: Arial, Helvetica, sans-serif;
}

h2 {
    color: white;
}

/* Style the header */
header {
    background-color: #2B547E;
    padding: 30px;
    text-align: center;
    font-size: 35px;
    color: white;
}

/* Create two columns/boxes that float next to each other */
nav {
    float: left;
    width: 30%;
    height: 300px; 
    background: #43BFC7;
    padding: 20px;
}

/* Style the list inside the menu */
nav ul {
    list-style-type: none;
    padding: 0;
}

article {
    float: left;
    padding: 20px;
    width: 70%;
    background-color: #f1f1f1;
    height: 300px;
}

/* Clear floats after the columns */
section:after {
    content: "";
    display: table;
    clear: both;
}

/* Style the footer */
footer {
    background-color: #2B3856;
    padding: 10px;
    text-align: center;
    color: white;
}

@media (max-width: 600px) {
    nav, article {
        width: 100%;
        height: auto;
    }
}
</style>


<header>
  <h2>Voice of the Theatre</h2>
<img src="http://pngimg.com/uploads/world_map/world_map_PNG14.png" width="100px" height="60px">
</header>

<section>
  <nav>
    <ul>
      <li><a class="one" href="#">EMEAR</a></li>
      <li><a class="two" href="#">AMER</a></li>
    </ul>
  </nav>
  
  <article>
    <h1>London</h1>
    
    <div id="pchange">
    
    </div>
    
<ul>
  <li>Update 1 </li>
  <li>Update 2</li>
  <li>Update 3</li>
</ul>


<h1>America</h1>
<ul>
  <li>Update 1 </li>
  <li>Update 2</li>
  <li>Update 3</li>
</ul>


  
  </article>
</section>

<footer>
  <p></p>
</footer>

Answer №1

To disable the functionality of a link, you have two options: either remove the href attribute from the anchor tag or add event.preventDefault() to prevent the default behavior.

Additionally, if you simply want to change the text content of an element, use .text() instead of .html().

var ptext;

$(document).ready(function() {
  ptext = $("#pchange");
  $(".one").click(function(event) {
    event.preventDefault();
    ptext.text("text1");
  });
  $(".two").click(function(event) {
    event.preventDefault();
    ptext.html("tex2");

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
  * {
    box-sizing: border-box;
  }
  
  body {
    font-family: Arial, Helvetica, sans-serif;
  }
  
  h2 {
    color: white;
  }
  /* Style the header */
  
  header {
    background-color: #2B547E;
    padding: 30px;
    text-align: center;
    font-size: 35px;
    color: white;
  }
  /* Create two columns/boxes that floats next to each other */
  
  nav {
    float: left;
    width: 30%;
    height: 300px;
    /* only for demonstration, should be removed */
    background: #43BFC7;
    padding: 20px;
  }
  /* Style the list inside the menu */
  
  nav ul {
    list-style-type: none;
    padding: 0;
  }
  
  article {
    float: left;
    padding: 20px;
    width: 70%;
    background-color: #f1f1f1;
    height: 300px;
    /* only for demonstration, should be removed */
  }
  /* Clear floats after the columns */
  
  section:after {
    content: "";
    display: table;
    clear: both;
  }
  /* Style the footer */
  
  footer {
    background-color: #2B3856;
    padding: 10px;
    text-align: center;
    color: white;
  }
  /* Responsive layout - makes the two columns/boxes stack on top of eac...
 
</footer>

Answer №2

In order to enhance the functionality, you have the options of adding href="javascript:void(0)", event.preventDefault(), or event.stopPropagation(); you can select any one method.

Your script is responsible for modifying the HTML content. It functions with .html() or .text(), but using .text() is recommended because it specifically refers to the inner text instead of elements in the HTML.

Initially, when you click on the nav ul li a button, extract the text and update the target element's text accordingly.

All systems are operational...

Appreciate your assistance

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

Instructions for concealing and revealing a label and its corresponding field before and after making a choice from a dropdown menu

Currently, I am working on developing a form that will enable customers to input their order information. This form includes a selection list for payment methods, with three available options. If the user chooses either credit or debit card as the paymen ...

Encase children within a view component in React Native

I've been working on the following code: renderActionButtons(actionButtons){ let actionButtonsContent = actionButtons.map((button, i) => { return <TouchableHighlight key={i} onPress={() => {this.updateConversation(button);}} style= ...

Adjusting the decimal points of numbers within a table to create the illusion of a "centered" alignment within the cell

Looking for a table design featuring a column of decimal numbers, each with varying lengths before and after the decimal point, while keeping the decimal points aligned. The width of the column should be flexible, expanding to accommodate long numbers in ...

Is there a way to prevent users from selecting certain days in ion-datetime?

After searching through the official documentation, I couldn't find a solution. I am in need of a function similar to the jQuery datepicker beforeshowday function. My goal is to disable all weekends (Saturday and Sunday) in upcoming dates so that user ...

"Using Flexbox to Align Child Elements of Varying

After learning about flexbox, a pressing question arose: How can I create a layout of columns similar to the one shown in this image? Check out MyCodeFiddle for more details! (http://jsfiddle.net/1s3bo913/) Click here to view the layout project: Layout Pr ...

The CSS styling is not being rendered correctly on the AngularJS HTML page

Encountering a puzzling situation here. Our angular controller is successfully delivering data to the page, but we are facing an issue with rendering a table due to an unknown number of columns: <table border="1" ng-repeat="table in xc.tables"> ...

The Ladda spin animation continues spinning for an additional second after the stop() function is called

I employ the ladda spinner in this manner: var l = Ladda.create(document.getElementById('ladda-test')); l.start(); l.stop(); console.log('ladda is stoped'); The issue I am facing is that following the execution of l.stop(), the animat ...

fullpage.js: the content exceeds the height limit

I am currently working on customizing the jquery script fullpage.js for a website built on the French CMS "SPIP" (). This script is used to create a one-page website with both horizontal and vertical navigation. However, I have encountered an issue with ...

Having trouble getting the jQuery script to properly check file size in an HTML form before uploading?

I've created an HTML form with a jQuery script to verify the file size when the SAVE button is clicked. Despite my efforts, the check doesn't seem to be functioning properly in the HTML Form. Please assist me with this and thank you in advance ...

Creating a new component when a click event occurs in React

Currently diving into the world of React while working on a project that involves mapbox-gl. I'm facing an issue where I can successfully log the coordinates and description to the console upon hover, but I can't seem to get the popup to display ...

Is there a method to have JavaScript display all the information during a SQL while loop?

I'm currently working on implementing a timer for my script. However, I am facing an issue where the timer only counts down from the last result instead of each individual result returned from the query. Any assistance with resolving this problem woul ...

The jsPdf library performs well on medium-sized screens like laptops, but when downloaded from larger monitor screens, the PDF files do not appear properly aligned

In the code snippet below, I am using html2canvas to convert HTML to PDF. The PDF download works fine on medium screens, but when viewed on larger screens, some pages appear blank and the content order gets shuffled. How can I resolve this issue so that th ...

What are the advantages of using history.push or another method from react-router-dom compared to simply assigning the path to window.location.pathname?

When I need to navigate within my code, I find it more convenient to simply assign the desired path to window.location.pathname. Can this approach have any drawbacks? ...

Adding space around a label in a React component with TypeScript

Here is the code snippet I am working with: const ParentComponent = () => { const name = "name1"; const type = "type1"; const label = `${name} ${type}`; //something to be done here to add some space // before the text return ( ...

What steps can I take to transform this jquery code into a react.js equivalent?

Can someone help me convert this jQuery code into ReactJS? I've been trying, but encountering a lot of errors. I'm new to jQuery and having trouble understanding it. In simple terms, this is how the code works: When I click on the text "Laptop, ...

Using Angular 4 to delete selected rows based on user input in typescript

I am facing a challenge with a table that contains rows and checkboxes. There is one main checkbox in the header along with multiple checkboxes for each row. I am now searching for a function that can delete rows from the table when a delete button is clic ...

Ensuring consistent placement and scrollability of two divs across all screen sizes

I am in need of a solution to fix the top and bottom divs in the given image. The scroll should only occur when there is overflow. <!DOCTYPE html> <html> <head> <script src="//code.jquery.com/jquery-1.9.1.min.js"></script> ...

Here's a new take on the topic: "Implementing image change functionality for a specific div in Angular 8 using data from a loop"

I need to create a list with dynamic data using a loop. When I click on any item in the list, I want the image associated with that item to change to a second image (dummyimage.com/300.png/09f/fff) to indicate it's active. This change should persist e ...

Moving elements upwards and downwards using CSS transitions

I am currently designing a metro tiles menu for my website, without using JavaScript and only relying on HTML and CSS. The issue I am facing involves the sliding tiles and the direction in which they slide. Additionally, there seems to be a problem where ...

Using CSS to Showcase the Art Gallery Page

This is my unique gallery display: https://i.stack.imgur.com/RddD8.png Here is the look I am going for https://i.stack.imgur.com/kuOye.png I want to showcase images in a slightly messy yet awesome way However, I encounter an issue when some images have ...