Loading identical code in two different div elements

I am in the process of designing a comprehensive resource database featuring a side-scrolling container. Once a user clicks on a thumbnail within the container, the contents of a corresponding div will fade in and display specific category content. Each div element is structured like this:

<div>
<h2>Category1</h2>
<p><a style="float:right" class="a_demo_four" href="/Resources/file1.pdf" target="_blank">
                        Download
</a>File Desc</p> 
<hr/> 
</div>

This content loading scenario applies to multiple categories, and I aim to have the ability to showcase all such contents collectively under a "View All" tab. My attempt to use jQuery's load function for achieving this looked like this:

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
<script> 
$(function() {
$("#includedContent").load("b.html"); 
}); 
</script> 
</head>

<body>
 <div id="includedContent"></div>
 <h1>This is why I rule</h1>
</body>
</html>

Despite my efforts, I have been unsuccessful in loading original div content into the "View All" category. Unfortunately, my knowledge of Javascript/jQuery is limited, which makes it challenging for me to reuse content across different divs without resorting to manual copying and pasting. This practice would not be sustainable in the long run, especially when adding new files, necessitating code edits in multiple places.

Your assistance is greatly appreciated!

Answer №1

To store your content in a variable, follow these steps:

let myContent = '';
$(document).ready(function(){
  // Load content from b.html into a div
  $('#includedContent').load('b.html', function(result){
    myContent = result;
    // Now you have the data from b.html saved in the variable myContent
    // You can use it elsewhere like this
    $('#anotherDivId').html(myContent);
  });
});

Answer №2

If you want your code to run when the document is ready, you can use the following method:

$(document).ready(function(){
    $("#includedContent").load("b.html"); 
});

However, if you need to add the content in multiple places as required, it's recommended to keep it stored somewhere accessible.

Check out this working jsfiddle example: http://jsfiddle.net/c5SAH/show/

This demo loads content from: http://jsfiddle.net/gfgE8/show/

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

Best Practices for Monitoring Actions in EmberJS: Detecting Clicks

I have recently delved into learning emberjs and I am facing challenges in understanding how different components interact with each other, as well as grasping the best practices for implementation. My goal is to utilize ember's provided class bindin ...

Liquid Static Content

What is needed to fix the fluid-fixed layout issue? HTML: <div class="header"> <div class="title">Title</div> <div class="options">Opt</div> </div> CSS: .header{margin:0;padding:0;} .title{margin-right:50px; ...

Is it possible to include spaces in a JavaScript alert message?

Is it possible to add spaces in an alert message? I have been trying to include spaces in my alert messages, but the alerts do not appear when there are spaces. Example where it works: https://jsfiddle.net/yczrhztg/ Example where it doesn't work: ht ...

Firebug does not receive a JSON object from jQuery when the JSON is generated by PHP

The JSON data stored in test.json contains: {"foo": "The quick brown fox jumps over the lazy dog.","bar": "ABCDEFG","baz": [52, 97]} Upon using the jQuery.ajax() method to process this static JSON from test.json, $.ajax({ url: 'test.json', ...

Sticky header and a sidebar gallery with consistently sized thumbnails

Hello everyone, I'm stepping into the world of HTML and CSS with my first question. As a beginner, I find myself facing a challenge that seems to be beyond my current skill level. Here's the scenario: I want to create a webpage with a fixed left ...

The css values for component _nghost-c0 in an Angular application

Recently, I've been delving into Angular 5 and couldn't help but notice the peculiar html tags with ng generated attributes like _nghost-c0 and _nghost-c1... This got me wondering, what exactly do these attributes signify? [_nghost-c3] .employee ...

Setting up Next.js configuration for JSX file type

I have developed a Next 13 application and installed the package @viral-loops/widgets. However, upon running the application, I encountered the following error: error - ./node_modules/@viral-loops/widgets/dist/react/Widget.jsx Module parse failed: Unexpec ...

Understanding the time complexity of Object.entries()

Is the complexity of Object.entries() in JavaScript known? According to information from this question, it seems like it could possibly be O(n) if implemented by collecting keys and values as arrays and then combining them together? ...

Is It Possible for an HTML Page to Load Within an iframe?

Recently, I embedded an iframe link from a game hosted on Scirra onto my website. However, I noticed that the page automatically scrolls down to where the iframe is positioned once opened, which is quite annoying. Does anyone know how to resolve this iss ...

Executing a function in React JS triggered by an event

Can someone help me figure out how to properly call a function from a React component within the same class? I am working with create-react-app. SomeFunction(){ return true; } handleClick(e){ SomeFunction(); // This part doesn't w ...

in Vue.js, extract the style of an element and apply it to a different element

Currently, I am using VUE.js 2 in my project. Here is my website's DOM structure: <aside :style="{height : height()}" class="col-sm-4 col-md-3 col-lg-3">...</aside> <main class="col-sm-8 col-md-9 col-lg-9" ref="userPanelMainContents" ...

Having difficulty in getting a hyperlink to open in a new tab with _blank attribute

I'm currently working with this code for my link. <a href="https://dribbble.com/jamesfrewin"><img class="socialicon" src="images/icons/dribbble_dark.png" onmouseover="this.src='images/icons/dribbble_pink.png'" onmouseout="this.src= ...

Adjust the image size using CSS within the containing outer div

I am trying to adjust the width of an image using css within typo3. Unfortunately, I only have access to the outer div container which is the custom border of the content element. To make the change, I added the following line to my css file: .Bild-Starts ...

Creating a Music Bingo game to ring in the New Year

Looking to create a Music Bingo game for New Year's Eve with randomized songs that can be selected, but experiencing an issue where nothing happens when you have 4 in a row. Attempted adding a submit button, but it doesn't trigger any action. Ide ...

Arranging objects in an array using jQuery for optimal organization

How can I rearrange the items based on their 'name' property? staticdata.items = [ {id: '0', 'name': 'ABC'}, {id: '0', 'name': 'XYZ'}, {id: '0', 'name': ' ...

I'm attempting to convert an HTML table to a pandas dataframe, but I'm running into an issue where I'm receiving the error message: TypeError: Cannot read object of type 'WebElement'

Trying to save an html table into a pandas dataframe is giving me trouble. I keep getting the error TypeError: Cannot read object of type 'WebElement'. driver.get('web_url') driver.maximize_window() driver.find_element_by_x ...

hiding elements yet passing down

Despite disabling the style sheet, a dynamically created form is displaying strangely. This form will show or hide various details based on selected options. For instance, many of the elements are generated with C#.net... formOutput += "<div class=&bs ...

Having trouble with a link not working correctly after concealing the file extension with htaccess?

I've configured the ht-access file to hide the .php file extension. Here's the code inside the .htaccess file: # Apache Rewrite Rules <IfModule mod_rewrite.c> Options +FollowSymLinks RewriteEngine On RewriteBase / # Add trailing slash to U ...

The assertion that 'd3Ctrl' is not a valid function, but instead is undefined

Although several people have raised the same issue before and I've attempted their solutions, I still can't seem to resolve it. My problem involves using d3, but I keep encountering this error: app.js var app = angular.module('myApp', ...

Opera browser having trouble with sidebar rendering

On my best days, I struggle immensely with troubleshooting browser display issues. The left-hand sidebar on this page () appears incorrect in Opera. While in all other browsers, the sidebar displays with an orange and tan background, for some unknown reas ...