Display the contents of a <div> tag from one HTML file onto another HTML file

Recently I embarked on learning HTML and came across a peculiar doubt. My goal is to create a section div on the first page that changes dynamically based on the menu item clicked, rather than redirecting to another HTML page. I've experimented with some JavaScript solutions but unfortunately haven't been successful, even though it seems to have worked for others in similar scenarios.

This is what I have so far:

.sideNav {
position: fixed;
width: 250px;
height: 100vh;
left: 0;
right: 0;
background-color: red;
top: 60px;
}

.contentWrapper {
margin: 60px 0 0 250px;
padding: 0 30px;
left: 0;
top: 0;
background-color: blue;
}
<!DOCTYPE html>

<html lang="en">
<head>
<meta charset = utf-8>
<link rel = "stylesheet" href = "./assets/stylesheets/main.css">
<script type="text/javascript">
function toggleMenu(id) {
var menuBox = document.getElementById(id);
if(menuBox.style.display == "block") {
menuBox.style.display = "none";
} else {
menuBox.style.display = "block";
}
}
</script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $(".menuAnchor").on("click", function() {
    event.preventDefault();
    $("#contentWrapper").load("food.html" + " #contentWrapper");
        return false;
        )};
        )};
</script>
<title>The Nest</title>


</head>
<body>
<aside class = "sideNav">
<div>
<a class = "menuAnchor"  onclick = "toggleMenu('foodMenu')"> Food</a>
<ul class = "menuBox" id = "foodMenu" style = "display : none">
<li>
<a id = "burguer" href = "#">Burguer</a>
</li>
<li>
<a id = "hotDog" href = "#">Hot Dog</a>
</li>
<li>
<a id = "pizza" href = "#">Pizza</a>
</li>
</ul>
</div>
</aside>
<div id = "contentWrapper">
<section class = "contentWrapper">
d
</section>
</div>
</body>
</html>

This is the other HTML file:

<!DOCTYPE html>

<html lang="en">
<body>
<div id = "contentWrapper">
<section class = "contentWrapper">
<h1>Food</h1>

<article id = "burguer">
<h2>Burguer</h2>
<p>
Burguer
</p>
</article>
<article id = "hotDog">
<h2>Hot Dog</h2>
<p>
Hot Dog
</p>
</article>
<article id = "pizza">
<h2>Pizza</h2>
<p>
Pizza
</p>
</article>
</section>
</div>
</body>
</html>

I aim to implement functionality where clicking a button triggers a cascade effect in the menu and updates the content of the div section to display information from the second HTML file instead of directing to a separate page.

While certain elements of my scripts seem promising, particularly those related to loading div sections from other pages, I'm facing challenges getting them to work seamlessly together.

I'm uncertain whether my current approach is optimal or if there's a more efficient way to achieve my desired outcome.

Thank you for your assistance and understanding as I navigate through this process.


My objective is to establish a main HTML page with header and menus:

Visual representation of the main HTML layout

Upon selecting one of the menu links, such as 'Food', the content should be dynamically replaced with information from another HTML file, like food items.

Answer №1

The information you're requesting is a bit unclear, but I can provide a solution based on my interpretation of your query:

If you wish to include HTML code that will replace the contents of a div, you may want to consider using the template tag. This tag allows you to define a template that is not displayed but can be accessed through jQuery (which appears to be in use here). Here's an example:

<div id="replacement-area">
</div>
<template id="replacement-content">
    <ul>
        <li>This</li>
        <li>Is</li>
        <li>Some</li>
        <li>Content</li>
    </ul>
</template>

jQuery:

$("replacement-area").html($("replacement-content").clone().html());

In this case, "replacement-area" would display the final content visible to the user, while "replacement-content" contains the content to be inserted into "replacement-area."

As a result, you would see:

<div id="replacement-area">
    <ul>
        <li>This</li>
        <li>Is</li>
        <li>Some</li>
        <li>Content</li>
    </ul>
</div>
<template id="replacement-content">
    <ul>
        <li>This</li>
        <li>Is</li>
        <li>Some</li>
        <li>Content</li>
    </ul>
</template>

With only the div being shown on the page.

I hope this explanation helps you achieve your goal.

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

The watch functionality is failing to work within a specialized attribute directive that is being utilized within a separate custom element directive

I'm currently working with two directives that handle separate functionalities. I am attempting to utilize the attribute directive within an element directive, but I'm encountering issues with the watch function not working on the attribute direc ...

Overcoming Troublesome Login Input Box in Web

In an effort to streamline tasks in our office, I am working on automating certain processes. Specifically, this program is designed to log into our insurance company's website and extract information for payroll purposes. Below is the code snippet th ...

Show Angular if it is in the array

Presently, I have an ng-repeat function to display comments on articles. It is structured like this: <div ng-repeat="comment in comments"> <li class="item" ng-class-even="'even'"> <div class="row"> ...

Learn how to find and filter elements in arrays that do not include a particular value using React

In my collection of recipes, I have the ability to filter out the ones that include specific ingredients. However, when I attempt to reverse this process by using .some method, it only checks the first item in the array. Here is an example of my data stru ...

What is the best way to access a variable from a .js file?

Is there a way to access a variable defined in a JavaScript file from a Vue file and pass it to the Vue file? In the following code snippet, there is a template.js file and a contact.vue file. The template file converts MJML to HTML and saves the output to ...

GraphQL/Relay Schema "Field cannot be queried..."

I'm encountering an issue when trying to query specific fields in graphql/relay. My goal is to retrieve the "courseName" field for Courses from the schema provided below. For instance, if I query the User with: { user(id:1){firstname,lastname}} T ...

Using MIPS Assembly Language: Displaying Register Content on Screen

I'm working on replicating a simple form of debugging in MIPS similar to javascript. I am wondering how I can achieve the equivalent of this ($t0 represents a javascript variable here): console.log($t0); In simpler terms, I am looking for the metho ...

Dragend event for images does not trigger in webkit when using TinyMCE

When using the TinyMCE editor, I tried binding the dragend event on images with the following code: _imagePlugin.editor.dom.bind(_imagePlugin.editor.dom.select('img'), 'dragend', function(){console.log('aaaa');}); Oddly enou ...

Save the array as a variable in your JavaScript code so that you can easily access it

I am currently working on generating a list only when a specific page is visited using JS/jQuery. I then need to be able to access this list when navigating to other pages and retrieve the variables within it. How can I effectively store this list? Initia ...

Ways to keep the position of an expanded collapsible table from Material UI intact

I found a collapsible table code snippet on this website: https://mui.com/material-ui/react-table/#collapsible-table However, there seems to be an issue where when I expand a row, the table "grows up" as it increases in size. This behavior is not ideal. I ...

Guide to activating MySQL query in Express

After creating a Nodejs Express app with a button on the client side, I want to execute a SQL query on the server side when the button is clicked. Is there a method to achieve this? The code in index.ejs: <button onclick='add()'>+1</but ...

PHP variables not being properly passed back to AJAX/jQuery POST on success despite using json_encode()

I've been grappling with this issue for hours. I am attempting to make a <div id=""data_friends> and a hidden input field update dynamically using AJAX. Here is the structure of the div tag: <div class="friends-tab-list" id="data_friends"> ...

The Mat-slide-toggle resembles a typical toggle switch, blending the functionalities of

I am facing an issue with a `mat-slide-toggle` on my angular page. Even though I have imported the necessary values in the module, the toggle is displayed as a normal checkbox once the page loads. HTML: <div style="width:100%;overflow:hidden"> < ...

How to efficiently handle a queue of JSON calls and callbacks using jQuery

This Question May Have Been Asked Before: Sequencing ajax requests I am faced with the task of retrieving songs by various artists through an array of artist names using Ajax calls to a remote webservice. My main concern is ensuring that all the ajax ...

Why isn't the HTML template opening in Outlook 7?

Does anyone know how to fix the code issue on Outlook 7? I've only included the header section of my code here, but please let me know if you need more information. I would appreciate any help with this problem. Thanks! ...

Ensuring the authenticity of user login credentials

I have a form in my HTML where the user needs to input their name and password. <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name"><br> Password: <input type="text" name="password ...

Gathering information from a website where the URL remains constant even after clicking on a specific button

Visit this website for bus tickets: I am working on scraping data related to bus trips from the mentioned site. However, the data I need to scrape depends on the user-selected date in my web application. Does anyone have suggestions on how I can develop ...

Is it possible to transfer state to the getServerSideProps function in any way?

As a newcomer to next.js, I have a question about passing page state to getServerSideProps - is it achievable? const Discover = (props) => { const [page, setPage] = useState(1); const [discoverResults, setDiscoverResults] = useState(props.data. ...

Is it possible to turn off the fallback color for bourbon radial gradients?

Is there a way to prevent the fallback from being applied when using the radial gradient mixin in Bourbon? I've consulted the documentation at , which states that the fallback is optional. However, no matter what I attempt, it seems that the primary ...

Using Node.js to display the outcome of an SQL query

I have been attempting to execute a select query from the database and display the results. However, although I can see the result in the console, it does not appear on the index page as expected. Additionally, there seems to be an issue with the way the r ...