Rearrange DIV elements by clicking a button that redirects you to a different webpage

Consider a scenario with two distinct pages:

  1. website.com/page1
  2. website.com/page2

Page 1 contains multiple buttons, each leading to Page 2.
On Page 2, there are various content sections (divs).

Is there a straightforward method to display specific content sections on Page 2 based on the button clicked on Page 1? I recall something about redirection being involved?

For instance, if Button 1 is clicked, only sections 2 and 3 will be visible on Page 2, but if Button 2 is clicked, only section 1 will be displayed on Page 2.

Answer №1

This answer may seem extensive, but I will do my best to simplify it for you.

Essentially, the solution involves utilizing ajax - specifically $.load - in order to retrieve content from another page.

Imagine you have the following HTML structure in site1:

<button id="button1" class="btn" data-load="#div1">Load Div1 from Site 2</button>
<button id="button2" class="btn" data-load="#div2">Load Div1 from Site 2</button>
<div id="loadContent"></div>

Using the onclick event of these buttons, you can extract the value of data-load, construct a valid URL, and then pass it to $.load. Here is an example:

$('.btn').on('click',function(){
   var url="/site2"+$(this).data('load');
   // The 'url' variable will be www.mysite.com/site2#div1 or www.mysite.com/site2#div2 depending on the clicked button
   $('#loadContent').load(url,function(){
      // Content from #div1 in site2 will be loaded within the loadContent element
      // Callback function after content is loaded
   })
   // or simply $("#loadContent").load(url);
});

Answer №2

Customize each button to send a unique query string to second website indicating the permitted div elements (e.g. mysite.com/site2?div1=true&div2=false)

Make sure to adjust the query string in site2's javascript code for proper handling...

Answer №3

If you want to customize the elements on your second page, you can assign specific id's to them and then proceed as follows:

To redirect to your page, use the following button:

<button onclick='window.location="page2.html#yourid"' >click me</button>

Include the following script in your page2.html:

var url=document.URL;

if(url.substr(url.indexOf("#"))=="#id1")
{
    //perform action for id1

}
else
{
    if(url.substr(url.indexOf("#"))=="#id2")
    {
        //perform action for id2
    }
    else
    {
        if(url.substr(url.indexOf("#"))=="#id3")
        {
            //perform action for id3
        }
    }
}

Make sure to replace the id's with your chosen ids and specify the actions you want to take for each id:

document.getElementById("id#").remove();

Answer №4

The issue lies within a WordPress page where the button appears in this manner (site 1):

 <div class="col-sm-12" onclick="window.location.href='<?=get_permalink($child->ID)?>'">
                                    <div class="r-targets-block-content">
                                        <div class="r-targets-block-bg" style="background:url('<?=$thumbnail?>') center top no-repeat"></div>
                                        <div class="r-targets-descr-block">
                                            <div class="r-targets-title"><?=$child->post_title?></div>
                                            <div class="r-targets-subtitle"></div>
                                            <div class="r-targets-button-block">
                                                <?=r_arrowed_button(
                                                    get_permalink($child->ID),
                                                    "SEE MORE", 
                                                    ["standard", "small", "border-yellow", "arrow-right"]
                                                )?>
                                            </div>
                                        </div>
                                        <div class="r-targets-recomendation-block-null"></div>
                                    </div>
                                </div>

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 XPath for the element that comes before a specific text

Looking for the XPath of the following code snippet: <a class="account-name" long-text="Sample Text" ng-click="accountClick(account)" ng-href="sample URL" href="sample URL1"> <span>Plan Name</span></a> I attempted this: //span[te ...

Pressing the button updates the value in the input field, but the input field continues to

I need some assistance with my e-commerce platform. I am trying to implement a button that adds items to the shopping cart, but I'm encountering an issue where the value in nbrSeats (my list of values) changes in the data, yet the input field displays ...

Issue with Ajax submit button failing to pass ID

I am encountering an issue while trying to update data using bootstrap modal. When the CGridView selectionChanged event is triggered, it should call a function to display a modal dialog form and populate the form with the selected data. Below is my CGrid ...

Navigating in CodeIgniter

I am facing a challenge with redirecting to another page in codeigniter, below is my JavaScript code snippet: <script type="text/javascript> $(document).ready(function () { var url = $('#baseurl').val(); var form = $(& ...

What causes the timer to pause, and what steps can be taken to avoid it? (Using Javascript with Iframe)

On my website, I have a page where clients must view an advertisement for 20 seconds. The website is displayed in an iframe with a countdown timer above it. I've set it up so that the timer stops when the window loses focus to ensure the client is ac ...

Help with jQuery .each(): ensuring completion before running subsequent lines of code

I am facing an issue with a jQuery function that is supposed to fetch data from the third column of a table, perform some operations on it, and then update the field with the result. Below is the code snippet in question: function getData() { $(&apo ...

What is the best way to insert a JavaScript variable into a filter while using ng-repeat?

I'm currently working on a situation where I have the following code: <div ng-repeat="i in MediaFeedItems | filter: { category: 'userSelect' }" class="mediafeed--item"> This specific code snippet is responsible for iterating through ...

Placing text over an image appears to be causing the navigation links to vanish - could it be a conflict with floating elements?

As a beginner, I'm facing a rather embarrassing situation here. Picture me banging my head on the table level of embarrassment... but let's give it a shot anyways. I've got a navigation bar and an image underneath (not behind). Both seem to ...

Custom Homepage with Integrated Google Web Search and Autocomplete Feature

Is there a way to incorporate the standard Google web search with autocomplete into my own webpage, without using a custom search engine like www.google.com? I have been unable to find any examples or guides on how to accomplish this. All the resources see ...

Utilizing Bootstrap 5 Grid to blend fixed and dynamic width elements within a single row

I am currently working on developing a grid system for my Blazor application that features small fixed-size columns with one large column. Instead of dividing the grid into 12 columns using col-xs-*, I have opted to set custom widths without using the col ...

Connect select with an array of objects and showcase information when an item is chosen from the dropdown menu

I have an array $scope.items= [ { name: "Jon Snow", email: "jon@example.com", password: "WinterIsComing" }, { name: "Daenerys Targaryen", email: "daenerys@example.com", password: "FireAndBlood" } ]; Now, I am trying to show the email value in a dropdown ...

Deploying an AngularJS 1.x application bundled with Webpack to Tomcat server

I've scoured the depths of Google and combed through the official documentation for Webpack, but I’ve yet to stumble upon a tutorial, guide, or plugin that addresses this particular issue. Does anyone have any experience with this problem, or should ...

Mysterious and never-ending loop that seems to loop endlessly and eludes my

My prototype includes a method for adding callbacks: /* * Add a callback function that is invoked on every element submitted and must return a data object. * May be used as well for transmitting static data. * * The callback function is supposed to e ...

Creating a json object using data from an HTML table

I am dealing with HTML returned from a web service that I cannot control. The HTML structure returned by the web service looks like this: <table id="result"> <tbody> <tr> <td><b>Name</b></td> < ...

Display all elements that come before and after the current element

For debugging purposes, I need to output the IDs of all previous images in a check. The issue arises when trying to assign classes to all previous elements using the script below, as the images are not being added to the correct classes. $(document).on(&a ...

I currently have two responsive menus and I'm trying to figure out how to modify the javascript so that when one menu is opened, the other

I am facing an issue with my responsive menus on a webpage, similar to the example provided in the jsfiddle link below. Currently, when one menu is open and I click on another, both remain open. How can I modify the JavaScript code so that when one menu op ...

Error: The function replace cannot be used on elem

Here is what I am attempting to achieve: for loop inside the append is not working Below is my code: $('body').append( $('<section>').append( $('<form>') .attr('class', "ac-custom a ...

Saving the created PDF documents on the server

Utilizing the jsPDF plugin, I am currently generating a .pdf file upon clicking a button to initiate a download. Rather than downloading the file, I would like it to be saved onto my server when the button is clicked. Specifically, I want the file to be sa ...

How can I align two inner boxes vertically in the most efficient manner?

.dd{ height:100px; width:100%; border:1px soild red; background:#000; } .dd .d1{ height:20px; width:20px; border:1px solid green; display:inline-block; } .dd .d2{ height:20px; width:20px; border:1px solid green; display:inl ...

What is the best way to use jQuery AJAX to make changes to an HTML element that will be permanent even after the page is refreshed?

Starting out with codeigniter, I am working on building an ecommerce website. Every time a user clicks the "Add to cart" button in my view, I utilize jquery ajax to send a request to a controller function. This function then returns two variables: count ( ...