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

Restrict Text Input Field to Accept Only Specific Domain

Hey there! I've set up a text input box for users to link their Tripadvisor page to their profile. I want to make sure that when they paste the URL in, it is checked for the correct format. For example, if someone pastes: or , then allow the link. Ho ...

Subscribe to a Mailchimp list by using Ajax

I'm attempting to sign up for my Mailchimp list using this form: <form method="post" action="https://us10.api.mailchimp.com/2.0/lists/subscribe.json" id="mailChimpForm"> <div class="col-lg-6 col-sm-6" data-scrollreveal="enter left after 0 ...

When trying to serialize elements from a form loaded by Firefox using AJAX, encountering difficulties due to

Visit the live version of this at . When prompted for a formId, input BroadRunTrack2013. Follow by adding an item to cart and entering player name information (you can use random data for now). Select the Runner's Hat, choose color/size, specify quant ...

Angular - send multiple HTTP requests for the length of an array and combine the responses into one array

Exploring angular for the first time and dabbling with the trello API. I have an array containing some list IDs that I need to make HTTP GET calls for, equal to the length of the array. For instance, if there are two IDs in the array, then the HTTP call sh ...

Issues with ellipses not functioning properly have been identified specifically on Safari when using the line

Currently, I am using the line-clamp feature to restrict the titles on a blog to only two lines at most. This functionality works perfectly fine on Firefox and Chrome browsers; however, when it comes to Safari, the ellipsis appears in the middle of the sen ...

What sets asyncData apart from methods in Nuxt.js?

I am currently utilizing asyncData to fetch data from an API, however it is restricted to pages and cannot be used in components. On the other hand, methods can be used in both pages and components. As these two methods function similarly, I am consider ...

The jQuery onchange event does not remove the disabled attribute from HTML elements

I have been experiencing some difficulties trying to remove the disable attribute from an HTML document using the prop method. Here is a snippet of my HTML code: <form> <!-- Add your HTML code here --> </form> Currently, I am only using ...

React component making an Axios request only receives the initial state as a response

I'm struggling with creating an AJAX call using Axios in React. Despite my efforts, I can't seem to pinpoint where the issue lies. Below is what I currently have within my component: ComponentDidMount() { axios.get('https://jsonplacehol ...

Retrieve data from the table and dropdown menu by clicking a button

A script is in place that retrieves data from two columns (Members, Description) dynamically from the table upon button click. Table html Here is the JQuery code responsible for extracting values from the table: $(function() { $('#myButton') ...

Leverage the power of Laravel8 to simultaneously update two tables in one go

In my database, I currently have two tables: https://i.stack.imgur.com/TObcy.jpg https://i.stack.imgur.com/U6IhT.jpg The form that I am working with is shown below: <form id="contratform" method="post" action="contrats" ...

Ways to effectively pass arguments to the callback function within the catch function in JavaScript

While working on my code, I suddenly felt the need to pass an extra argument, "msg", to the callback function renderError(). This extra argument should be passed along with the default error argument generated by the catch function itself. I tried doing i ...

Managing a high volume of HTTP requests within a React-Redux application

Seeking assistance with my react-redux app project. I have a substantial amount of data that has been divided into smaller files, allowing the user to choose a start and end time range. Upon clicking the "Fetch data" button, I create HTTP request promise ...

Issue encountered with the DevExtreme npm module: Uncaught TypeError - $(...).dxButton is not recognized as a function

Instructions for installing DevExtreme npm can be found on their official page here: https://www.npmjs.com/package/devextreme var $ = require('jquery'); require('devextreme/ui/button'); var dialog = require('devextreme/ui/dialog&a ...

The NPM version needs to be updated as it is outdated

Struggling with a dilemma here. My Laravel project is quite dated, and I'm unable to execute npm run dev. Let's take a look at some code: php artisan laravel --version: Laravel Framework 5.8.38 node --version: v16.16.0 This is the current Node v ...

Is the JQuery Mobile .page() method triggering an endless loop?

Creating a dynamic listview with data from an AJAX response has been successful, however, when using JQM's .page() function on it, it appears to enter an infinite loop where the listview keeps getting appended indefinitely. I'm unsure if this is ...

Is it possible for me to listen to an AngularJS event using regular JavaScript, outside of the Angular framework?

Is it possible to listen to an event triggered in AngularJS using regular JS (outside of Angular)? I have a scenario where an event is being emitted using RxJS in Angular 2. Can I observe that event from pure JS? Here's some example pseudo code: imp ...

Error: Browserify jQuery Plugin Not Working

Having trouble browserifying a jQuery plugin and keep seeing this error in the browsers console: Uncaught Error: Cannot find module 'jquery' Here's how I have my package.json set up: "browserify": { "transform": [ "browserify-shim" ...

Does one require the express.js framework in order to create a web application using nodeJS?

Exploring the creation of a basic web application using HTML, NodeJS, and Postgres. Can this be achieved without incorporating the ExpressJS framework? Seeking guidance on performing CRUD operations with NodeJs, Javascript, and Postgres sans ExpressJS. G ...

Ensuring Proper Highlighting for HTML Select Options

I am working on a project involving an HTML Drop-down menu. Check out the code I have so far: http://jsfiddle.net/dineshk/u47xjqLv/ HTML Code <select> <option class="first" style="display:none;">HH</option> <option class= ...

In search of the most efficient method for integrating an AJAX-powered TreeGrid feature

Can anyone recommend an Ajax/TreeGrid implementation that meets the following criteria: Must support server side sorting Should be able to load leaf nodes on demand, meaning only children of open nodes are loaded Needs to support paging so that nodes are ...