What is the best way to dynamically load a hyperlinked file into a specific div on a webpage?

Can anyone help me with targeting a specific element on the same page rather than opening in a new tab? I want to display content below when clicking on links like Home, Food, or Resort. I have tried searching online but couldn't find a solution that works for me.

Here are the links I'm referring to

<div id = "panel">
    <p>
        <a href = "home.html" target = "display">
            <font color ="white">Home</font
        </a>
        <a href = "resort.html" target = "display">
            <font color ="white">Resort</font>
        </a> 
        <a href = "hotel.html" target = "display">
            <font color = "white">Hotel</font>
        </a> 
        <a href = "food.html" target = "display">
            <font color ="white">Food</font
        </a> 
        <a href = "rates.html" target = "display">
            <font color="white">Rates</font>
        </a> 
        <a href="reservation.html"target="display"
            <font color="white">Reservation</font>
        </a> 
        <a href = "contact.html" target = "display">
            <font color = "white">ContactUs</font>
        </a>
    </p>
</div>

This is where the content should be inserted

<div id="display">
    <iframe name="display"></iframe>
</div>

Answer №1

If I am grasping the query - even though it's a bit unclear to me - it seems like your issue can be easily resolved. Simply modify this:

<name = "display">

To:

<iframe name="display">

I'm assuming you want to embed those links within an iframe, correct? Otherwise, achieving what you desire would involve utilizing JavaScript. However, my understanding is not completely clear.

There is no <name> tag in HTML, but there is a name attribute

Edit for clarification on iframes:

Iframes load one webpage B inside of webpage A, while remaining separate pages. The "box" likely refers to the border of the iframe itself. The scrollbar appears when the content of webpage B exceeds the display capacity of the iframe. To eliminate the scrollbar, you can either enlarge the iframe or decrease the content size of webpage B.

Expanding an iframe is similar to enlarging any other element: simply adjust its CSS.

Answer №2

I attempted to implement this solution by using the following code snippet: JsFiddle Link:

Start by including jQuery at the top of your code:

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

Then, add the following jQuery code at the bottom of the file. This code listens for a click event on anchor tags and prevents the default action of submitting the link. It also uses the load function to fetch and display content based on the "href" attribute of the clicked anchor tag.

<script>
$("a").on("click", function(){
//alert($(this).attr("href"));  
$( "#display" ).load( $(this).attr("href") );
return false;
});
</script>

Note: This solution works fine when running on a local server or loading .php files. However, if you are accessing it through Windows Explorer or a Linux file system, make sure to open it in Firefox.

Answer №3

A definite yes to crowhill's suggestion. It seems like you're aiming for a single-page website. In that case, you'll need to compile all your content into one HTML page using various divs for each section of content. Then, use links with the respective div IDs in the href part of your anchor tags (example: ``).

I believe this approach will address your concerns and give you the desired outcome.

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

I'm encountering an issue in Atom where it says "Module 'editorconfig' cannot be found."

I keep encountering an issue in the Atom text editor where it says "Cannot find module 'editorconfig'" every time I try to save a .html file. Even though I had previously installed the EditorConfig package without any problems. How can I resolve ...

CSS Rounded edges and shadow not functioning in IE or Chrome

I noticed that my CSS Rounded Corners and Shadow work perfectly fine when I debug in Visual Studio using Chrome. However, when I upload it to a server and try to access it through Chrome or IE, the shadow and rounded corners do not display. Is there someth ...

Learn the technique of initiating one action from within another with Next Redux

I'm looking to set up user authorization logic when the page loads. My initial plan is to first check if the token is stored in the cookies using the function checkUserToken. Depending on whether the token is present or not, I will then call another f ...

What is the method for setting autofocus to the first input element within a loop?

I am currently working on a loop to display inputs, and I would like to be able to add focus to the first input element when it is clicked. Does anyone have any suggestions on how I can select that first element and set autofocus on it? ...

Is it possible to dynamically change the background image using ReactJS?

I attempted to dynamically change the background image style for the div below: Below is my code snippet for implementation: render: function() { var divImage = { backgroundImage : "url(" + this.state.song.imgSrc + "),url(" + this.state.nextImgSrc ...

Respond to the initial message within the "if" statement

client.on('message', message => { if (message.channel.type === 'text' && message.channel.name.toLowerCase() == "channel-2" && message.content === "test") { message.react("✅") } ...

React component not consistently returning correct results due to undefined values being included

Currently, I have a function that iterates through one array, comparing its values with those in another array. When it finds a match, it adds the matching results to a new array called addToState. These matches are then concatenated with the original arra ...

Are Your Padding Styles Missing?

I've noticed that the text under the photos on this page in the main section (a.event) is not displaying the 5px padding top and bottom. Any suggestions for fixing this? Thank you! =) a.event { width:315px; height:auto; border:0; padding: 5px 0; } ...

"Encountering critical issues while executing gulp in the terminal

https://i.sstatic.net/gobwG.png When running the gulp command, I keep encountering these errors. What could be causing this issue? SyntaxError: Unexpected token : at exports.runInThisContext (vm.js:54:16) at Module._compile (module.js:375:25) at Ob ...

How to utilize Node.js to pause execution and wait for a specific function to

In my coding scenario, I am dealing with a specific code snippet that is part of a larger if statement block: message.reply({ embeds: [multipleUsersEmbedFunction("Multiple Users Found", `The following users were found:\n${string}`, members)] ...

Is it possible to determine the search query if the search term appears within the website URL?

I'm trying to figure out which action url and name term to use when the search term is located in the middle of the URL. For instance, the HTML code below enables me to type text and upon pressing enter or the button, it redirects me to the website w ...

The AJAX(ASP.NET MVC) system is unable to locate the requested image

I am currently working on implementing an AJAX call for my project Here is the code snippet: <script> $(document).ready(function () { email_update(); }); function email_update() { $.ajax({ url: '@Url.Action("EmailsList", "Ques ...

The data from the Subscribe API call is gradually loading within the ngOnInit() function

When using Angular 8, I am experiencing slow data retrieval when making API calls in the ngOnInit() function. The issue arises when trying to pass this data as @Input from one component module to another - it initially comes through as undefined for a minu ...

Using jQuery to dynamically add a class based on the resolution of the browser window

Hello everyone, I need assistance with adding a class to the body dynamically based on the browser window resolution. I have some code that I am attempting to use, but I am not familiar with jQuery and could use some guidance. The specific outcomes I aim ...

With Firefox, when submitting a form only the final repeated region value is $_POSTed and not all of them

There seems to be an issue with the code below when running it in Firefox. The problem occurs when passing the URL variable UserName. In Firefox, only the last record is being used from the record-set, regardless of which button is clicked. This page utili ...

The 'AutocompleteDirectionsHandler' cannot be utilized until the Google Api has been initialized

Attempting to utilize the Google Maps and Autocomplete API is proving challenging. The error encountered reads: Uncaught (in promise) ReferenceError: Cannot access 'AutocompleteDirectionsHandler' before initialization at window.initMap (traf ...

Interpret the JSON reply

Could someone please explain why my function B() is not responding? <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/ja ...

How can AngularJS handle sorting based on the start date and end date?

Is there a way to filter the items by Start and End Date, based on the invoice_date, using the date range functionality in a meanjs app? I am facing an issue where the date filter functions work perfectly in Plunker and my localHost production environm ...

Issue with displaying data using a custom pure pipe and boolean in ngIf condition

For my current web project, I require a friendship/follow feature. There are two roles involved: admins and regular users. Regular users have the ability to follow other users, while admins do not possess this capability. When a user wishes to follow anot ...

Generating conference itinerary - establishing agenda function (Sequelize)

const _ = require('lodash'); const Promise = require('bluebird'); const Sequelize = require('sequelize'); const ResourceNotFound = require('./errors').ResourceNotFound; const ResourceAccessDenied = require('./er ...