Substitute the outdated body element with the freshly loaded element

Using ajax with .load() to load page contents.

The formatting of my HTML pages is as follows:

For the home page:

<html>
  <head></head>
  <body class="HOME">
    <header></header>
    <div id="content">
      //CONTENT
    </div>
  </body>
</html>

For article pages:

<html>
  <head></head>
  <body class="ARTICLE">
    <header></header>
    <div id="content">
      //CONTENT
    </div>
  </body>
</html>

Loading only the content inside the div element with id #content. When transitioning from an article page to the home page, I need the body element to update the class from #HOME to #ARTICLE.

After loading the content, manually setting the new body class like this:

jQuery('body').attr('class', 'HOME');

Desiring a method to automatically retrieve and set the class of the new body when loading content. Something along the lines of:

var newBodyClass = jQuery('body').load('/ body').attr('class');
jQuery('body').attr('class', newBodyClass);

Answer №1

Remember to delete the existing code before adding this new one.

jQuery('.link').on("click", "a", function() {

 jQuery('body')
    .find("#content")
        .fadeOut(10, function() {

            jQuery('#content').hide('slow').empty().load('/' + " #content", { is_ajaxed_page: "yes" }, function() {

                jQuery('#content').fadeIn(10, function() {

                });
            });
        });

    return false;
}); 

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 it possible to determine if a HTML element has word-wrap applied, causing a word to break in the middle of the text?

Inside a div, I have Persian text that wraps because I've used word-wrap: break-word; and the div's size is smaller than the text. <div class="r25" style="width: 13.3333px; height: 50px;"> <p style="font-size: 8px;">This is an ex ...

The button effortlessly transforms into a sleek email submission form

I have a button that is already styled with basic CSS properties for active and hover states. I would like to add functionality so that when the "Join the Loop" button is clicked, it transforms into a simple email form similar to the one found at Apologie ...

Implement custom styles to enhance the appearance of users' Magento websites

Is it possible to add additional CSS only to users/customers who are logged into our webshop? If so, how should I go about it - through XML or PHTML? Stian ...

Issue with JQuery: Fancybox not functioning properly within Google Maps

While working with a Google Map, I encountered an issue where I needed to allow users to expand images within the map itself. Fancybox seemed like the perfect solution for this, but unfortunately it is not functioning as expected. Link to example In the ...

Struggling to save a signature created with an HTML5 Canvas to the database

I've been on the hunt for a reliable signature capture script that can save signatures to MySQL, and I finally found one that fits the bill. However, there are two issues that need addressing: The canvas doesn't clear the signature when the c ...

How to alter row colors in SQL/PHP tables

echo "<tbody"; echo "<tr>"; echo "<td>{$id}</td>";// display customer Id echo "<td> {$firstname} {$lastname}</td>"; //display customer title,firstname,lastname echo "<td>{$date->format('h:i A')}</td> ...

Divide a string into separate array items where a specific character is found

Within my sentence are several characters (~). I want to split the text before each ~ into an array item, and then add a <br /> tag at the end of each item. I attempted using the replace method but it only worked for the first ~ and disregarded the ...

Creating Eye-Catching Images by Incorporating Image Overlays Using Just One Image

I'm facing a bit of a challenge here. I need to figure out how to overlay an image onto another image using just a single image tag. Specifically, I want to add a resize icon to the bottom right corner of an image to let users know they can resize it ...

Improving Performance of a Large Unordered List using JavaScript

My website currently features a search box that retrieves images and displays them in a list format. Each image has an associated click event that triggers an overlay on the parent li element when clicked. However, with search results exceeding 300 images ...

What is the best way to align my two buttons in the center?

I'm having trouble aligning my two buttons to the center. Additionally, I'm not sure how to remove the gray color from inside the buttons. Here is the code: index.html <!DOCTYPE html> <html> <head> <met ...

Starting up with card and a loop counter

Can anyone help me with getting my card bootstrapping implemented in my .html to be centered? Here is the code I'm working with: <h1> 10 Games Recommended Based on {{ selected }} </h1> <div class="container"> ...

What is the best way to update a local variable in JavaScript using Ajax requests?

function validate_authentication(){ var is_authenticated = false; $.ajax({ type: "POST", url: "/account/islogin/", data: "", async: "false", success: function(data) { if (data == "null") { ...

I'm noticing a significant slowdown on my webpage due to a high volume of href URLs in my HTML script. What steps can I take to resolve this issue

As a newcomer to scripting, I spent countless hours researching to find a solution but couldn't crack it. I implemented a jquery image search script that functioned smoothly with all components embedded in a single HTML code. However, things took a t ...

"Enhancing web design with jQuery mousemove to dynamically adjust CSS filters according to mouse location

I'm attempting to create a simple mousemove event that changes the CSS filter property of my background div based on the position of the mouse. While I have successfully achieved this in the past with background-color, I am now encountering issues wit ...

What is the method for populating a select2 widget using AJAX?

After inputting a few characters into my widget, I receive AJAX results that look like this: [{"id":550,"campName":"IB Resi Showtime Rebate Website"},{"id":60,"campName":"OB ACGBK EB"}] Despite this, I am unsure of how to populate the widget with the obt ...

Discovering relative URLs within an MVC 4 framework

Seeking a solution for storing the fully qualified URL of a relative path in MVC: ~/Content/themes/base/jquery-ui.min.css" In my scenario, I have a hidden input field: <input type="hidden" id="siteUrl" value=""/> I attempted to populate this hidd ...

The parent div isn't properly extending to accommodate the table, and the columns aren't aligning correctly within the table when using CSS flexbox

I have a scenario where I need a table inside a div to take up the entire space of the div, with the second column and its inputs expanding as much as possible to fill the width (i.e. col1+col2 = div width). Due to being nested within another div, absolut ...

Is there a way to create an image gallery layout similar to Pinterest using CSS?

I am currently developing a dynamic PHP gallery that will feature thumbnails with the same width but varying heights. These thumbnails will be arranged from left to right, so I would prefer not to use a traditional five-column layout. I suspect that achiev ...

JSF View Scoped bean persists after redirecting to JSP page

I am facing an issue with my app that performs AJAX updates on the page and uses a @ViewScoped ManagedBean. After the user submits the form, the action method redirects to a JSP to display a chart using the Cewolf library. Surprisingly, the bean seems to s ...

Fetching data from a database using Jquery Autocomplete

Creating a pop-up form to add items and implement jQuery autocomplete has been successful in my project. However, I am facing an issue where the form fields for brands, type/series, and serial numbers should also appear when I enter the item name like "Lap ...