Adam Zeigler Adam Zeigler - 3 months ago 38
HTML Question

Capturing href and prevent event default

Not sure why the code is not working. The javascript is not printing the alert box nor preventing the event default. I was under the assumption that the data would load asynchronously keeping me on the same page.

<!DOCTYPE html>
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type='text/javascript' src=''></script>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<div data-role="page">
<div data-role="header"></div>
<div data-role="content">
<p id = "heading">Is Nursing For You?</p>
<div id = "div1" align="center"></div>
<div data-role="footer" id = "foot" data-position="fixed">
<th><h1 id = "alignLeft">Future Goals</th></h1>
<th><h1 id = "socialMediaText alignCenter">Get Social With Us!</h1>
<td id = "alignLeft">Telephone: 304-444-39876</td>
<td = "alignLeft">Email:</td>

This is where im having the trouble at. Not sure why the alert is not sending or redirecting me.
<script type='text/javascript'>



This is the Fp.php which is called when the page is loaded but once the page is loaded there are href's that are displayed. so when the person clicks the href the page will stay but the data will change and get the info coming from articles.php (at the bottom).

$sqlPAQuery = "SELECT pages.pageId, pages.pageTitle FROM pages order by
$paqueryResult = mysqli_query($conn,$sqlPAQuery);
while ($paqueryRow = mysqli_fetch_object($paqueryResult))

$pages = "<div class='center-wrapper'><a href = articles.php?
pageId=".$paqueryRow->pageId."><button class= center-wrapper'>".$paqueryRow-
echo $pages;



$sqlARTICLEQuery = "SELECT * FROM articles where pageId=".$_GET['pageId']."
order by articleId";
$articlequeryResult = mysqli_query($conn,$sqlARTICLEQuery);
while ($articlequeryRow = mysqli_fetch_object($articlequeryResult))
$articles ="<div id = 'div1' class='center-wrapper'><a href =
article.php?articleId=".$articlequeryRow->articleId."><button id

echo $articles;

Answer Source

You need to wait for the div contents to load before manipulating it. Try this:

$(document).ready(function() {
    $("#div1").load('FONWVhp.php', function() {
        $('#div1 a button').click(function(event){

Also, I've corrected some mistakes in your code:

  1. Instead of $('#div1').click(...) use $('#div1 a button').click(...).
  2. I suppose you want the anchor's href, so you need to use alert($(this).parent().attr('href')).
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download