Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Javascript onclick issue

Featured Replies

Well, I've broken my dependence on Dreamweaver and jumped into the deep end of the pool. Now I'm up against something that I was absolutely sure should work, and I'm stumped. I am trying to use Javascript to load an image and display it in a div when a button is pushed. It works just fine if I use an absolute address for the image, but when I use a variable nothing happens. I trace the variable and it is returning the correct value. Every indication is that this should work, but it just does nothing. Please help.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html lang = "en">
<head>
	<meta charset="utf-8">
	<title>List of Parks</title>
	<link href="guide.css" rel="stylesheet" type="text/css">
	<style type="text/css"></style>

	<script type="text/javascript" src="../jQuery/jQuery.js"></script>
	<script type="text/javascript">

	<!--
		function showImage()
	{
		document.getElementById('loadingImage').style.visibility='visible';
	}

	-->

	</script>

</head>

<body>
<div id="container">
	<div id="state"></div>

	<div id="list">		
		<?php foreach ($datas as $name): 
			{
			if ($name['state'] == 'PA')
			{
		?>	

		<input type="hidden" name="id" value="<?php echo $name['id']; ?>">

		<h2><?php echo htmlspecialchars($name['name'], ENT_QUOTES, 'UTF-8');?></h2>
		<?php htmlspecialchars($name['site'], ENT_QUOTES, 'UTF-8');?>
		<?php $link = $name['site']; ?>
		<ul id="link">
		<li class="l1"><?php echo "<a href=$link>$link</a>" ?></li>
		</ul>
		<br>
		<?php echo htmlspecialchars($name['description'], ENT_QUOTES, 'UTF-8');?>
		<?php echo htmlspecialchars($name['street'], ENT_QUOTES, 'UTF-8');?>
		<br>
		<?php echo htmlspecialchars($name['city'], ENT_QUOTES, 'UTF-8');?> ,
		<?php echo htmlspecialchars($name['state'], ENT_QUOTES, 'UTF-8');?>
		<?php echo htmlspecialchars($name['zip'], ENT_QUOTES, 'UTF-8');?> 

	<?php $file = $name['id'];
	$image = '../maps/'.$file.'.gif';?>	
	<input type="button" value="click for map" onclick="showImage();"/>

		<div id="trailmap">
			<img id="loadingImage" src="<?php $image ?>" style="visibility:hidden"/>
		</div>

		<?php echo $image?>

	<hr width="100%" size="3" black />	


		<?php 
			}
			}
			endforeach; ?>			
		</div>
</div>

<div class="fixbox">
<div id="statemap"></div>
<div id="home"></div>
<div id="guide"></div>
</div>

</body>
</html>

 <img id="loadingImage" src="<?php echo $image ?>" style="visibility:hidden"/>

 

You're missing the echo command that puts the php variable into the HTML

Edited by Sogo7

  • Author

Now this is interesting. I changed the code to make the name of the button the file name to see what would happen, and the name of the button does change with the iteration. $image does change correctly in every single circumstance except when I use that variable to try to load the image.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.