My experience in Ajax is minimal. I've managed to create two <li>
elements with <a>
elements inside, each linking to different HTML files. My goal is for clicking on a link to automatically load the corresponding HTML file without refreshing the page, but it's not functioning as expected. When I click on a link, nothing occurs. Appreciate any assistance! Using jQuery with Ajax.
FIRST HTML FILE
$('li').find('a').on('click',function(event) {
event.preventDefault();
var href = $(this).attr('href');
$('.main-content').load(href + 'content-ajax');
});
.ajax-img {
margin-top: 8em;
float: left;
}
.ajax-img2 {
float: left;
margin-left: 10em;
margin-top: -5em;
}
.ajax-nadpis {
display: flex;
justify-content: center;
align-items: center;
height: 800px;
font-size: 2.7em;
}
.ajax-text {
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5em;
margin-top: -13em;
}
nav {
display: flex;
}
ul li {
display: inline-block;
padding-left: 18em;
margin-top: 2em;
}
ul li:hover {
cursor: pointer;
text-decoration: underline;
}
<!DOCTYPE html>
<html lang="en">
<head>
//FIRST HTML PAGE
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Responsive Website</title>
<link rel="stylesheet" href="css/styles2.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@100;400&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Cinzel&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Tinos&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Pacifico&display=swap" rel="stylesheet">
</head>
<body>
<h2 class="podnadpis">Automatic loading articles using ajax (without page refresh)</h2>
<section class="ajax">
<div class="section-container">
<div class="img-holder">
<img src="ajax-IMGs/talltales.png" class="tall-tales-logo" alt="logo tall tales">
</div>
</div>
</section>
<article id="article">
<nav class="navigation">
<ul>
<li><a href="ajax1.html">Shroudbreaker</a></li>
<li><a href="javascript.html">Skeleton Lords</a></li>
</ul>
</nav>
<div class="main-content">
<div class="content-ajax">
<img src="ajax-imgs/graymarrow.png" class="ajax-img" alt="">
<h3 class="ajax-nadpis">SKELETON LORDS</h3>
<p class="ajax-text">Tall Tales – Shores of Gold introduces Skeleton Lords, imposing characters clambering out of the oldest legends into the present day. Meaner than any of the bony marauders you’ve fought so far, these intimidating adversaries can only be encountered by following the story as it unfolds. Be prepared for anything when seeking out Skeleton Lords – these resourceful foes will resist any attempt to send them back to their resting place beneath the sands. They’re big, tough and full of surprises…</p>
</div>
</div>
</article>
<script src="javascript/jquery.js"></script>
<script src="javascript/jquery.parallax.js"></script>
<script src="javascript/main.js"></script>
</body>
</html>
SECOND HTML FILE
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Responsive Website</title>
<link rel="stylesheet" href="css/styles2.css">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@100;400&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Cinzel&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Tinos&&display=swap" rel="stylesheet">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Pacifico&display=swap" rel="stylesheet">
</head>
<body>
<article id="article">
<nav class="navigation">
<ul>
<li><a href="ajax1.html">Shroudbreaker</a></li>
<li><a href="javascript.html">Skeleton Lords</a></li>
</ul>
</nav>
<div class="main-content">
<div class="content-ajax">
<img src="ajax-imgs/shroudbreaker.png" class="ajax-img2" width=700px height=600px alt="">
<h3 class="ajax-nadpis">SHROUDBREAKER</h3>
<p class="ajax-text">The Shroudbreaker is the first Tall Tale in the Shores of Gold Arc. The Tale Book can be voted for on the table next to the Mysterious Stranger in the Tavern of any Outpost. The Tall Tale can be voted to be cancelled at the Ship's Voyage Table. Players can unlock three Checkpoints during this Tall Tale. If they wish to continue the Tall Tale at a later time, they can vote on any acquired Checkpoints at the Voyage Table.</p>
</div>
</div>
</article>
<script src="javascript/jquery.js"></script>
<script src="javascript/jquery.parallax.js"></script>
<script src="javascript/main.js"></script>
</body>
</html>
The issues I'm encountering are shown in this image: https://i.stack.imgur.com/0VgsT.png