How to nest a div within another div

I am attempting to nest a div tag inside another div tag using javascript. I am loading content from one page into a div tag and need the div tag to load the page as well as trigger the script when a user clicks on a menu image.

Javascript Code:

$('#menu-body a').click(function() {
    var page = $(this).attr('href');
    $('#body-wrap').load('content/' + page + '.php');
    return false;
});

HTML Code:

<head>
    <link rel="stylesheet" type="text/css" href="css/style.css" >
    <title>End of Us - Home</title>

    <script type="text/javascript">
        function MM_swapImgRestore() { //v3.0
            var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
        }
        function MM_preloadImages() { //v3.0
            var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
            var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
            if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
        }

        function MM_findObj(n, d) { //v4.01
            var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
                d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
            if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
            for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
            if(!x && d.getElementById) x=d.getElementById(n); return x;
        }

        function MM_swapImage() { //v3.0
            var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
            if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
        }
    </script>

</head>

<body onLoad="MM_preloadImages('img/home_over.png','img/store_over.png','img/support_over.png','img/forum_over.png')">

    <div class="page-wrap">
        <div class="menu-wrap">
            <div class="menu-box"><a href="index" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('home','','img/home_over.png',1)"><img src="img/home.png" width="134" height="65" id="home"></a><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('store','','img/store_over.png',0)"><img src="img/store.png" width="144" height="65" id="store"></a><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('support','','img/support_over.png',1)"><img src="img/support.png" width="190" height="65" id="support"></a><a href="#" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('forums','','img/forum_over.png',1)"><img src="img/forum.png" width="142" height="65" id="forums"></a></div>
        </div>

        <div class="body-wrap"></div>

        <div class="footer-wrap">
            <div class="footer-box">End of Us - Site by Gawdzahh</div>
        </div>
    </div>

    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="js/contentLoader.js"></script>
</body>

Answer №1

The body-wrap class is defined in your HTML, but you are referencing it as an ID in your JavaScript code. Update $("#body-wrap") to $(".body-wrap"), or change

<div class="body-wrap"></div>
to
<div id="body-wrap"></div>

Similarly, instead of using $("#menu-body a"), make sure to use $(".menu-box a"). There is no div with the ID of menu-body in your HTML.

Answer №2

Update your code to the following:

#body-wrap refers to the ID body-wrap of a div

<div id="body-wrap">

.body-wrap refers to the class body-wrap of a div

<div class="body-wrap">

  $('.body-wrap').load('content/' + page + '.php');

Answer №3

To begin with, familiarize yourself with the concept of class and id selectors in CSS.

In CSS, an id is indicated by using #, while a class is denoted by adding a . beforehand.

For more information, you can refer to this CSS CLASS & ID

When working on your HTML document,

  1. The class name body-wrap should be selected using .body-wrap.

  2. If there are no elements with the id menu-body in your HTML, then using #menu-body in jQuery as a selector is incorrect. Instead, consider using $(".menu-box a") to target anchor tags within .menu-box.

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

Is there a way to retrieve user input without having to submit the form?

I am looking to capture user input without the need for submission, and then pass it through an AJAX method as a parameter to an action. Despite trying various methods, I have not been able to find a solution. Below is the code snippet: <input type="t ...

A tutorial on how to switch classes between two tabs with a click event in Vue.js

I am having an issue with my spans. I want to implement a feature where clicking on one tab changes its color to red while the other remains gray. I have tried using class binding in my logic but it's not working. How can I solve this problem and make ...

Is there a method to categorize an array of objects by a specific key and generate a new array of objects based on the grouping in JavaScript?

Suppose I have an array structured like this. It contains objects with different values but the same date. [ { "date": "2020-12-31T18:30:00.000Z", "value": 450 }, { "date": "20 ...

What is the purpose of utilizing "({ })" syntax in jQuery?

What is the purpose of using ({ }) in this context? Does it involve delegation? Can you explain the significance of utilizing this syntax? What elements are being encapsulated within it? For instance: $.ajaxSetup ({ // <-- HERE error: fError, ...

Hearken to the modifications in the header of ag-grid

I referenced this example to create a custom header for my https://stackblitz.com/edit/clabnet-ag-grid-rich. Here is how I made modifications to it: I added a button that opens a modal popup to edit the header of a column. Everything works correctly, but ...

Active tab in HTML

In my implementation based on this example code from https://www.w3schools.com/howto/howto_js_tabs.asp, I have a specific requirement. I want the tab for "Paris" to remain active even after the page is refreshed if the user has clicked on the button for "P ...

The console displays the progress percentage, but it is not visible within the HTML

I am trying to troubleshoot an issue where file uploading progress is not displaying in the HTML, even though it is visible in the console. What could be causing this behavior? When I use console.log(this.progress), the progress is shown in the console bu ...

Creating a menu bar in jQuery that functions similarly to tabs, but without the need to change divs

Currently, I am exploring ways to create tab functionality similar to jQuery's tabs plugin but without the need to switch between DIVs. Essentially, I am looking for a jQuery plugin that solely handles rendering the tab bar and allows me to designate ...

Detecting drag events between connected angular ui-trees is a complex yet achievable task

How can I trigger an action when an item is dragged from tree#1 to tree#2 and dropped? I want to make a specific HTTP call to save the transferred item. I have successfully implemented actions within one tree using the 'dropped' event, but strugg ...

The Next.js Image component is not compatible with an external URL as the image source

I've been struggling to use the image component in Next.js with an external URL as the source, but I keep encountering an error. I followed the instructions in the official Next.js documentation and updated the next.config.js file, but unfortunately, ...

Accessing an Access database via HTML on a personal computer

Currently, I am exploring methods to establish a connection with an access database situated on my desktop using HTML. Most resources I've come across focus on server-side databases, suggesting tools like ASP.net or recommending the use of SQL or MySQ ...

Toggling the `toggleClass` on a fixed element when clicking a link (such as in an off canvas menu) can

As per the following markup code, there is a sticky navigation bar (menu-bar) that reveals an off-canvas menu using CSS transitions when the button is clicked by adding a class (slide-wrapper__open) to the HTML element. HTML <div class="menu-bar"& ...

Circular reference detected during JSON serialization

Two classes were created: "Candidate" and "Experience". A Candidate can have multiple experiences. public class Consultant { public int ConsultantID { get; set; } public string ConsultantNom { get; set; } public string ConsultantPrenom { get; ...

How to Implement Screenreader and Ontab Functionality for Hover Visibility Accessibility

Hello there! I am excited to share that I am currently in the process of creating my very first React app. The purpose of this project is to develop a writer's application that can be easily accessible to all users. As of now, I have successfully bui ...

Different Line Thickness in Dropdown Menu

Is there a way to adjust line heights in CSS within a drop down menu? I have attempted to change the font sizes of each element, but the new font size does not seem to take effect once an option is selected. Any suggestions would be greatly appreciated! & ...

Modifying state through inter-component communication in React.js

I need to implement a mechanism where the state of the main component can be updated from the "A" component, then passed to the "B" component for rendering and populating boxes dynamically. Main Component : constructor() { super(); this.s ...

In React/JavaScript, the array variable defined outside the component is having props added to it multiple times

After entering new data, I want to add it to an array called DUMMY_MEALS outside the component and display it as a list. The issue I am facing is that the 'data' object is being added multiple times to DUMMY_MEALS and rendering duplicates on the ...

The assets on the page are not loading inside their designated containers

While conducting functional tests with nightwatch.js, I encountered a discrepancy between my local environment and the containerized environment. In the container environment, which is crucial for integrating into my CI pipeline and running automated tests ...

Can a websocket be used to communicate with a server that is not the same as the one hosting the html5 website?

My goal is to establish communication between a hardware device and an HTML5 website. It seems like the best way to do this would be through WebSockets or possibly WebRTC over a WiFi network. Is that correct? I haven't come across any solutions invol ...

Utilizing Kendo MVVM to Bind to an Self-Executing Anonymous Module Function

Currently, I am experimenting with the Kendo UI MVVM framework and attempting to bind it to a self-executing anonymous modular function. The situation is a bit complex, as the functionality is only somewhat working. Although the module is being updated whe ...