You are here:  » Unhide content onclick

Support Forum



Unhide content onclick

Submitted by richard on Sun, 2008-12-07 00:01 in

Hi David

I am trying to use div tags to unhide a field on clicking a link plus opening a new page - typical of voucher code site (using modified search.php)

See code below for searchresults.php:

<table width='100%'>
    <?php foreach($searchresults["products"] as $product): ?>
   <tr>
  <td valign='top'>
          <?php if ($product["image_url"]): ?>
          <a onclick="document.getElementById('voucherCode').style.display='block';window.open('<?php print $product["productHREF"]; ?>');" href="javascript:;"><img border='0' src='<?php print $product["image_url"]; ?>' alt='<?php print $product["brand"]; ?>' /></a>
          <?php endif; ?>
</td>
      <td width='50'></td>
        <td valign='top'>
<!-- click to reveal -->
<a onclick="document.getElementById('voucherCode').style.display='block';window.open('<?php print $product["productHREF"]; ?>');" href="javascript:;">Click here</a>
<?php print $product["description"]; ?>
<div id="voucherCode" style="display:none;">
<h1>************************</h1>
<h1><strong><span style="color: #ff0000;">
Voucher Code: <?php print $product["category"]; ?>
</span></strong></h1>
<h1>************************</h1>
Source: <?php print $product["brand"]; ?>
</div>
<!-- end click to reveal -->
        </td>
</tr>
<?php
   endforeach; ?>
</table>

However, the onclick does not reference the appropriate row in the results, it always defaults to the first result.

I have tried many iterations without any success.

How would you tackle this problem?

Many thanks

Submitted by support on Sun, 2008-12-07 10:01

Hi Richard,

I think that's happening because this section of code is within a foreach() loop (for each search result); but you are using the same DIV name for each row ("voucherCode"). What you need to do is use a different name for each row, for example voucherCode1, voucherCode2 etc. However, I think I would simply make use of the $product["id"] variable, and append that to voucherCode. You should only need to change the 2 points in the code where voucherCode is referred to, so in place of:

document.getElementById('voucherCode')

...use:

document.getElementById('voucherCode<?php print $product["id"]; ?>')

...and in place of:

<div id="voucherCode" style="display:none;">

use:

<div id="voucherCode<?php print $product["id"]; ?>" style="display:none;">

Cheers,
David.

Submitted by richard on Sun, 2008-12-07 11:15

Hi David

Perfect :)

Once again, many thanks